Re: Checking if an image

From: Ed Astle (REEMOVE_ed_at_REEMOVE_thesignpost.demon.co.uk)
Date: 04/20/04


Date: Tue, 20 Apr 2004 01:32:46 +0100

I had to implement the exact same thing under ASP a few years ago.

I used FindMimeFromData(), which is part of the Internet development stuff
(urlmon.h). It checks the actual content of a data buffer or a url to
determine what the mime type of the data is. From what I remember IE knows
what the initial chunk of various file types "looks like". Jpgs have one
type of header, exe another etc. Give it at least 512 bytes from the front
of a file and it should be able to figure out what it is.

As for doing the same under dotnet - not sure. A quick seach in msdn brings
up stuff like HttpPostedFile.ContentType. That may just report the HTTP
"content-type" field (that the posting brower would set) or it may also
check the actual content - you'll have to try it.

Generally you want to determine the mime type (mime = Multipurpose Internet
Mail Extensions). I know you're not doing email but "mime type" is what
you're after.

BTW - you can explicitly set the "accept" types to prevent the brower
posting stuff the server doesn't want. The web server informs the browser
what mime types are acceptable. See HttpWebRequest.Accept etc. eg
accept="image/jpeg,image/tiff" or "image/*" (can't remember if its ',' or
';' separators). It's still important to do the validation you describe
though.

Regards,
Ed.

"Steve Peterson" <speterson@arrakis.es> wrote in message
news:%23cNh71kJEHA.2244@tk2msftngp13.phx.gbl...
> Hi
>
> I have an ASP.NET where users can upload images to the server. I've
already
> implemented business logic that checks the file extension of the files
> uploaded to make sure they are of an acceptable image type. However, it
> occured to me that a devious user might simply give some non image (Like
an
> .exe) file to an image extension, upload it, and then cause havoc latter
> on.
>
> So, what I need to do is implement business logic that makes sure they
file
> uploaded IS an image file before saving to the server. I'm not familiar
with
> working with graphics, but I figure there must be some method in .NET that
> returns a boolean value if the file is an image format or something along
> those lines..
>
> Can anyone help me out??
> TIA
> Steve



Relevant Pages

  • Re: Checking if an image
    ... > determine what the mime type of the data is. ... > posting stuff the server doesn't want. ... >> I have an ASP.NET where users can upload images to the server. ... >> uploaded to make sure they are of an acceptable image type. ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: Mime type?
    ... MIME type should not affect the upload process. ... iis ftp log file? ... > IIS server, the problem is they are using McIntosh/Apple computer and they ...
    (microsoft.public.inetserver.iis)
  • Re: Upload image works fine unless image is edited first
    ... PHP upload won't upload the image. ... The mime type of the file, ... //Is the width of the original bigger than the height? ...
    (comp.lang.php)
  • Re: Upload image works fine unless image is edited first
    ... I didn't realize that editing a picture, ... PHP upload won't upload the image. ... The mime type of the file, ... //Is the width of the original bigger than the height? ...
    (comp.lang.php)
  • RE: MIME type
    ... The file you upload gets created on the server as an instance of ... this has a contenttype property. ... along with the file name in the DB, then when you stream it, you set the ... > Is there a way of detecting the MIME type of a file dinamicaly using .NET? ...
    (microsoft.public.dotnet.framework.aspnet)

Quantcast