Displaying advertisement web content from Respose.WriteFile ()/ Response.ContentType
How would certainly one present any kind of add web content from a "vibrant" aspx web page? Presently I am working with making use of the System.Web.HttpResponse "Page.Response" to write a documents that is saved on an internet server to an internet demand.
This would certainly permit individuals to strike a link to the type http://www.foo.com?Image=test.jpg and also have the photo display screen in their internet browser. So as you might recognize this focuses on making use of Response.ContentType.
By utilizing
Response.ContentType = "application/octet-stream";
I have the ability to present photos of type gif/jpeg/png (all i have actually examined until now), little bit attempting to display.swf or.ico documents offers me a wonderful little mistake.
making use of
Response.ContentType = "application/x-shockwave-flash";
I can get blink documents to play, yet after that the photos are messed.
So just how do i conveniently pick the contenttype?
This is hideous, yet the most effective means is to consider the documents and also set the web content type as ideal :
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;
}
This becomes part of a remedy I make use of on a neighborhood intranet. Several of the variables you will certainly need to accumulate on your own as I draw them from a data source yet you might draw them from elsewhere.
The only added yet I've acted is a function called getMimeType which attaches to the data source and also draws back the proper mine type based upon documents expansion. This defaults to application/octet - stream if none is located.
// 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();
Related questions