C# 显示Respose.WriteFile()/Response.ContentType中的广告内容

C# 显示Respose.WriteFile()/Response.ContentType中的广告内容,c#,C#,如何显示“动态”aspx页面中的任何添加内容?目前,我正在使用System.Web.HttpResponse“Page.Response”将存储在Web服务器上的文件写入Web请求 这将允许人们点击该类型的url,并在浏览器中显示图像。因此,您可能知道,这是围绕Response.ContentType的使用展开的 利用 Response.ContentType = "application/octet-stream"; 我能够显示gif/jpeg/png类型的图像(到目前为止我测试过的所有图像

如何显示“动态”aspx页面中的任何添加内容?目前,我正在使用System.Web.HttpResponse“Page.Response”将存储在Web服务器上的文件写入Web请求

这将允许人们点击该类型的url,并在浏览器中显示图像。因此,您可能知道,这是围绕Response.ContentType的使用展开的

利用

Response.ContentType = "application/octet-stream";
我能够显示gif/jpeg/png类型的图像(到目前为止我测试过的所有图像),但试图显示.swf或.ico文件会给我一个很好的小错误

使用

我可以播放flash文件,但是图像会被弄乱


因此,我如何轻松地选择contenttype?

这是我在本地intranet上使用的解决方案的一部分。当我从数据库中提取一些变量时,您必须自己收集这些变量,但您可以从其他地方提取它们

唯一的额外功能是一个名为getMimeType的函数,它连接到数据库并根据文件扩展名返回正确的地雷类型。如果找不到应用程序/八位字节流,则默认为应用程序/八位字节流

// 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();

这很难看,但最好的方法是查看文件并根据需要设置内容类型:

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; 
}
是的,丑陋但真实。最后,我将要使用的MIME类型放入数据库中,然后在发布文件时将它们取出。我仍然不能相信没有类型的自动回复列表,或者没有提到MSDN中提供了什么


我发现这个网站提供了一些帮助。

因为可以使用.NET4.5

MimeMapping.GetMimeMapping
它返回指定文件名的MIME映射

MimeMapping.GetMimeMapping