Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Best way to detect file is image?

Posted on 2010-11-24
6
Medium Priority
?
599 Views
Last Modified: 2012-05-10
Hello,

My web form allows users to upload files. If the file is an image, I like to show a thumbnail of the image.

The form allows word docs and pdfs to be uploaded too. For these, I dont display an image.

So, what is the best way to detect a file is an image and to display it in an <img /> control?

Currently I base it on extension. if (ext == ".gif" || ext == "png || img == jpg) img.ImageUrl = file.Name

That list could go on, including tif, bmp. So is there a smarter way?

thanks
0
Comment
Question by:Valimai
6 Comments
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 34210023
Another way is to check data in an image file, i.e. file’s mime-type.

Have a look:

http://www.bitrepository.com/how-to-validate-an-image-upload.html
0
 
LVL 6

Expert Comment

by:AlfredRobot
ID: 34210037
you can use the following javascript code in this link:
http://forums.asp.net/p/1590719/4029169.aspx

However, if you want to make it more precisely(e.g., some body changed the extension from jpg to doc), it's better for you to check the image file header:
http://www.mikekunz.com/image_file_header.html
0
 
LVL 30

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 200 total points
ID: 34210044
Also have a look at one possible way to check MIME types (lot of others on Internet):

http://stackoverflow.com/questions/2826808/how-to-identify-the-extension-type-of-the-file-using-c

List of MIME types:

http://www.webmaster-toolkit.com/mime-types.shtml
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 34210184
For what it's worth, I did a simple test page with a GIF, JPG, PNG, TIF, and BMP image.  Safari was the only browser that displayed the simple uncompressed TIF along with the rest.  IE6/8, Firefox, Chrome, and Opera displayed all the others.
0
 
LVL 9

Accepted Solution

by:
Subrat (C++ windows/Linux) earned 800 total points
ID: 34210294
You can use GetFileInfo() WIN32 API

System.IO.FileInfo fInfo = new System.IO.FileInfo(openFileDialog1.FileName)

or
Search in registry to get the extention of file
or
Can use  Path.GetExtension(string path)

or follow this link
http://www.codeproject.com/KB/cs/GetFileTypeAndIcon.aspx


After getting the extension, you can know whether it's an image file or not.
hints:

string extension = Path.GetExtension(FileInput.PostedFile.FileName);

switch(extension.tolower())
{
case ".gif":

case ".jpg":

case ".bmp":
             //Print it is an image file
             //here add all possible ext.s

................
}


or keep all the possible image file exts. inside an array and iterate through it.
It'd be more powerful if you will check the binary whether it's a valid imagefile or not
0
 
LVL 1

Author Comment

by:Valimai
ID: 34214745
Thank you all for contributing!

I had decided to detect the mime type of all files and have have implemented the registry method for now. It helped me in another area. One down side is the PDF example where PDF will only be detected if Acrobat reader is installed for example. So when the application is on the server, PDF is not someone you tend to install on that. So I may implement the list solution.

As for actually detecting an image, mime type is no good because you get mimetypes of "image/[foobar" when they are actually video files. So I have decided to detect based on extension. This only presents a problem when you detech JPG, but not the heritage extensions of JPE JPEG. Im not too concerned.

thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question