显示来自Respose.WriteFile()/ Response.ContentType的广告内容

时间:2020-03-05 18:37:59  来源:igfitidea点击:

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

这将使人们可以将URL键入http://www.foo.com?Image=test.jpg类型,并将图像显示在浏览器中。因此,我们可能知道这与使用Response.ContentType有关。

通过使用

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

我能够显示gif / jpeg / png类型的图像(到目前为止,我已经测试过所有),试图显示.swf或者.ico文件的位给了我一个很好的小错误。

使用

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

我可以播放Flash文件,但是图像混乱了。

那么,如何轻松地选择内容类型?

解决方案

回答

这是我在本地Intranet上使用的解决方案的一部分。当我从数据库中将它们拉出时,我们将必须收集一些变量,但我们可能会将它们从其他位置拉出。

唯一但我有的添加功能是一个名为getMimeType的函数,该函数连接到数据库并根据文件扩展名拉回正确的地雷类型。如果未找到,则默认为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();

回答

这很丑陋,但是最好的方法是查看文件并适当设置内容类型:

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; 
}

回答

Yup Keith丑陋但真实。我最终将将要使用的MIME类型放入数据库中,然后在发布文件时将其拉出。我仍然无法相信那里没有类型的强制性列表,或者没有提及MSDN中可用的类型。

我发现此站点提供了一些帮助。