从UNC路径读取文件并在HTTP请求中设置正确的MIME类型 [英] Reading a file from a UNC path and setting the correct MIME type in a HTTP request

查看:169
本文介绍了从UNC路径读取文件并在HTTP请求中设置正确的MIME类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何从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类型,你需要拥有自己的数据库,将文件扩展名映射为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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆