题
如何将一个显示器的任何添加内容从一个"动态"aspx页?目前,我正在使用该系统。网。HttpResponse"页面。响应"写的文件存储在一个网络服务器网页的请求。
这将允许人们击中一个网址的类型 http://www.foo.com?Image=test.jpg 和图像显示在他们的浏览器。所以你可以知道这种围绕采用的响应。ContentType.
通过使用
Response.ContentType = "application/octet-stream";
我能够显示图像类型gif/jpeg/png(我所有的测试迄今为止),比特试图来显示。swf。ico文件给了我一个不错的小错误。
使用
Response.ContentType = "application/x-shockwave-flash";
我可以得到闪光的文件发挥,但随后的图像都搞砸了.
所以我怎样 很容易 选择contenttype?
解决方案
这是丑陋的,但最好的方法是看文件和设置内容的类型,酌情:
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;
}
其他提示
这是解决方案的一部分,我利用在当地的内部网。某些变量收集自己作为我把它们从一个数据库,但是你可能把他们从别的地方。
只有额外的但我有一个函数 getMimeType 它连接的数据库和拉回正确的地雷类型的基于文件的扩展。这defaults to application/octet-stream如果没有找到。
// 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();
自那以后。净额4.5一个可以使用
MimeMapping.GetMimeMapping
它返回MIME映指定的文件名称。
https://msdn.microsoft.com/en-us/library/system.web.mimemapping.getmimemapping(v=与110).aspx
不隶属于 StackOverflow