21

How would one display any add content from a "dynamic" aspx page? Currently I am working on using the System.Web.HttpResponse "Page.Response" to write a file that is stored on a web server to a web request.

This would allow people to hit a url to the type http://www.foo.com?Image=test.jpg and have the image display in their browser. So as you may know this revolves around the use of Response.ContentType.

By using

Response.ContentType = "application/octet-stream";

I am able to display images of type gif/jpeg/png (all i have tested so far), bit trying to display .swf or .ico files gives me a nice little error.

using

Response.ContentType = "application/x-shockwave-flash";

I can get flash files to play, but then the images are messed.

So how do i easily choose the contenttype?

FryHard
  • 10,305
  • 7
  • 35
  • 38

4 Answers4

10

This is ugly, but the best way is to look at the file and set the content type as appropriate:

switch ( fileExtension )
{
    case "pdf": Response.ContentType = "application/pdf"; break; 
    case "swf": Response.ContentType = "application/x-shockwave-flash"; break; 

    case "gif": Response.ContentType = "image/gif"; break; 
    case "jpeg": Response.ContentType = "image/jpg"; break; 
    case "jpg": Response.ContentType = "image/jpg"; break; 
    case "png": Response.ContentType = "image/png"; break; 

    case "mp4": Response.ContentType = "video/mp4"; break; 
    case "mpeg": Response.ContentType = "video/mpeg"; break; 
    case "mov": Response.ContentType = "video/quicktime"; break; 
    case "wmv":
    case "avi": Response.ContentType = "video/x-ms-wmv"; break; 

    //and so on          

    default: Response.ContentType = "application/octet-stream"; break; 
}
Keith
  • 150,284
  • 78
  • 298
  • 434
0

Yup Keith ugly but true. I ended up placing the MIME types that we would use into a database and then pull them out when I was publishing a file. I still can't believe that there is no autoritive list of types out there or that there is no mention of what is available in MSDN.

I found this site that provided some help.

Vineet Jain
  • 1,515
  • 4
  • 21
  • 31
FryHard
  • 10,305
  • 7
  • 35
  • 38
0

This is part of a solution I use on a local intranet. Some of the variables you will have to collect yourself as I pull them from a database but you may pull them from somewhere else.

The only extra but I've got in there is a function called getMimeType which connects to the database and pulls back the correct mine type based on file extension. This defaults to application/octet-stream if none is found.

// Clear the response buffer incase there is anything already in it.
Response.Clear();
Response.Buffer = true;

// Read the original file from disk
FileStream myFileStream = new FileStream(sPath, FileMode.Open);
long FileSize = myFileStream.Length;
byte[] Buffer = new byte[(int)FileSize];
myFileStream.Read(Buffer, 0, (int)FileSize);
myFileStream.Close();

// Tell the browse stuff about the file
Response.AddHeader("Content-Length", FileSize.ToString());
Response.AddHeader("Content-Disposition", "inline; filename=" + sFilename.Replace(" ","_"));
Response.ContentType = getMimeType(sExtention, oConnection);

// Send the data to the browser
Response.BinaryWrite(Buffer);
Response.End();
GateKiller
  • 74,180
  • 73
  • 171
  • 204
0

Since .Net 4.5 one can use

MimeMapping.GetMimeMapping

It returns the MIME mapping for the specified file name.

https://learn.microsoft.com/en-us/dotnet/api/system.web.mimemapping.getmimemapping

Kols
  • 3,641
  • 2
  • 34
  • 42
shurik
  • 775
  • 9
  • 19