C# 显示来自 Respose.WriteFile()/ Response.ContentType 的广告内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Displaying ad content from Respose.WriteFile()/ Response.ContentType
提问by FryHard
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.
如何显示来自“动态”aspx 页面的任何添加内容?目前我正在使用 System.Web.HttpResponse "Page.Response" 将存储在 Web 服务器上的文件写入 Web 请求。
This would allow people to hit a url to the type http://www.foo.com?Image=test.jpgand have the image display in their browser. So as you may know this revolves around the use of Response.ContentType.
这将允许人们点击类型为http://www.foo.com?Image=test.jpg的 url并在他们的浏览器中显示图像。您可能知道这与 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.
我能够显示 gif/jpeg/png 类型的图像(到目前为止我已经测试过了),尝试显示 .swf 或 .ico 文件给了我一个很好的小错误。
using
使用
Response.ContentType = "application/x-shockwave-flash";
I can get flash files to play, but then the images are messed.
我可以播放 Flash 文件,但是图像一团糟。
So how do i easilychoose the contenttype?
那么我如何轻松选择内容类型?
采纳答案by Keith
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;
}
回答by GateKiller
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.
这是我在本地 Intranet 上使用的解决方案的一部分。当我从数据库中提取某些变量时,您必须自己收集它们,但您可以从其他地方提取它们。
The only extra but I've got in there is a function called getMimeTypewhich 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.
唯一额外的,但我已经得到了一个名为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();
回答by FryHard
Yup Keithugly 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.
是的,基思丑陋但真实。我最终将我们将使用的 MIME 类型放入数据库中,然后在发布文件时将它们取出。我仍然无法相信那里没有类型的自治列表,或者没有提到 MSDN 中可用的内容。
I found thissite that provided some help.
我发现这个网站提供了一些帮助。
回答by shurik
Since .Net 4.5 one can use
由于 .Net 4.5 可以使用
MimeMapping.GetMimeMapping
It returns the MIME mapping for the specified file name.
它返回指定文件名的 MIME 映射。
https://docs.microsoft.com/en-us/dotnet/api/system.web.mimemapping.getmimemapping
https://docs.microsoft.com/en-us/dotnet/api/system.web.mimemapping.getmimemapping