从 UNC 路径读取文件并在 HTTP 请求中设置正确的 MIME 类型 [英] Reading a file from a UNC path and setting the correct MIME type in a HTTP request
问题描述
我将如何从 UNC 路径读取文件、发现正确的 MIME 类型并将其流式传输到浏览器?
How would I go about reading a file from a UNC path, discovering the proper MIME type, and streaming that out to a browser?
在我看来,我正在重新发明 IIS,而且我还必须为每个文件扩展名维护我自己的 MIME 类型数据库.上述要求听起来合理吗,还是有更好的方法?
It feels to me like I'm re-inventing IIS, and I'll also have to maintain my own MIME type database for each file extension. Does the above request sound reasonable, or is there a better way?
我计划通过 IIS7 上的浏览器 HTTP Get 请求将其流式传输.如果重要的话,我也在同一台服务器上运行 Cognos.任何框架都可以(WCF、ASPX 等)
I plan on streaming this out via a browser HTTP Get request on IIS7. If it matters, I'm also running Cognos on the same server. Any framework is OK (WCF, ASPX, etc)
推荐答案
使用 WCF 非常基础:此代码可以托管在 IIS/Service/WAS/etc 下.
我从来没有找到一种方便的方法来处理 mime 类型,你需要有自己的 db 来将文件扩展名映射到 mime 类型.
Using WCF its pretty basic:
This code can be hosted under IIS/Service/WAS/etc.
I never found a convenient way to handle the mime type, you will need to have your own db that will map file extension into mime types.
[ServiceContract(SessionMode = SessionMode.NotAllowed)]
public interface IMediaRetriver
{
[OperationContract]
[WebGet(UriTemplate = "/get?f={fileName}")]
Stream Get(string fileName);
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class MediaRetriver : IMediaRetriver
{
public Stream Get(string fileName)
{
// pro tips
// this will cause the file dialog to show the file name instead of "get"
WebOperationContext.Current.OutgoingResponse.Headers.Add(
"Content-disposition", string.Format("inline; filename={0}", fileName));
WebOperationContext.Current.OutgoingResponse.ContentType =
"application/octet-stream";
// you want to add sharing here also
return File.Open(fileName)
}
}
这篇关于从 UNC 路径读取文件并在 HTTP 请求中设置正确的 MIME 类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!