1

I have a service returning an image stream to be displayed in UI. How do I set the ContentType value for this as I do not know what the image type will be? It can be image/jpeg, image/png etc.

fileContent.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("inline")
                    {
                        FileName = fileName
                    };
                    fileContent.Content.Headers.ContentType = new MediaTypeHeaderValue(??);
user5663970
  • 119
  • 2
  • 13
  • You can use `System.Web.MimeMapping.GetMimeMapping(fileName)`. See https://msdn.microsoft.com/en-us/library/system.web.mimemapping.getmimemapping.aspx – haim770 Dec 06 '16 at 07:24
  • Possible duplicate of [Get MIME type from filename extension](http://stackoverflow.com/questions/1029740/get-mime-type-from-filename-extension) – John Wu Dec 06 '16 at 07:25
  • 1
    @haim770 - filename is not available since the service returns only the byte stream – user5663970 Dec 06 '16 at 09:46
  • @user5663970, Might be an overkill, but: http://tika.apache.org/ (there's an unofficial .Net port too). – haim770 Dec 06 '16 at 09:48
  • Possible duplicate of [Determine file type of an image](http://stackoverflow.com/questions/55869/determine-file-type-of-an-image) – Alex K. Dec 06 '16 at 11:19

1 Answers1

0

One way to solve this is by restricting users the type of files they can upload.

If you don't want to do that just add file type to image/ and send it as mime-type.

example,

png : image/png gif : image/gif

You will also have to check whether the image is in compatible format for browser while sending. check this wiki page.

If it is not, then user is using some other application to process the image. So, you will have to use application/ and the file type. I strongly discouage use of this, because there could be only few selected type of images.

refer this for complete MIME-types. (Link)

Please Note: for jpg format, it should be jpeg.

Also, as mentioned by haim770, you can make use of System.Web.MimeMapping.GetMimeMapping which would return required mime-type, for all type of files, not just images.

Prajwal
  • 3,930
  • 5
  • 24
  • 50