[RESOLVED] Detect text file during uplaod

Hi,

on my website I have file uploading (only text files). How to detect correct file type? 

Reading first 8 bytes doesn't works here because there are text files.

Any ideas?

Did you try checking the extension?

string extenstion = Path.GetExtension(filename);
        if (extenstion != ".txt")
        {
        }

This approach is not safe.

Hi,

olo21

How to detect correct file type? 

There's several methods you can do to control the type of upload file.

One is to use validation controls, it can force the checking to be done on the client. Of course, it need the browser allow client side use of the validation controls.

Check:http://www.developer2blog.com/post/2011/02/07/File-Upload-Control-File-Type-Validation.aspx

Second is use server side code to check file type. This method allow the file upload to server, but before save the file, it will first check the type on server.

You can check:http://www.beansoftware.com/ASP.NET-FAQ/FileUpload-Validation.aspx

If you want to a more secure way. I suggest to use both client side and server side validation

Hope this can help you.

hi..

when uploading ,you wanto check the file extension means you have to use httpfileuploaded,try this link to know more aboout this function 

http://vbdotnetaddict.blogspot.com/2011/02/upload-image-or-file-into-database-in.html

Use RegularExpressionValidator and also in case use customValidator with serverside function to actually validate the text file...

Thanks.

All your solutions are good but there is one disadvantage: they work based on a file extension. Is any other way to detect text files?

Hi,

olo21

they work based on a file extension

If you do not want to check the extension. The way I can think out is to use the server code: FileUpload1.PostedFile.ContentType.

After text files post to server. Use above code to check the mine type(text/plain). But also notice that, if user changed the file extension manually. The mine type will not be correct as we expect. Seems that if I changed the txt to jpg or pdf, the mine type is correct. But like .doc or other extension. The type will changed also.

Hope this can help you.

Hi,

olo21

they work based on a file extension

If you do not want to check the extension. The way I can think out is to use the server code: FileUpload1.PostedFile.ContentType.

After text files post to server. Use above code to check the mine type(text/plain). But also notice that, if user changed the file extension manually. The mine type will not be correct as we expect. Seems that if I changed the txt to jpg or pdf, the mine type is correct. But like .doc or other extension. The type will changed also.

Hope this can help you.

thoughts on "[RESOLVED] Detect text file during uplaod"

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>