【问题标题】:Download File through headers from different server通过来自不同服务器的标头下载文件
【发布时间】:2016-04-27 02:51:22
【问题描述】:

我将 PDF 文件放置在不同的(文件服务器)服务器机器上,并且托管我的 MVC 应用程序的 IIS 机器对该文件服务器具有权限。从 IIS 机器我可以通过以下 URI 访问文件:

file://file-server/data-folder/pdf/19450205.pdf

我想让我的 MVC 应用程序的用户通过单击下载链接或按钮来下载他们各自的文件。所以可能我必须为那个链接/按钮写一些动作。

我尝试通过以下方式为我的 Action 方法使用 File 返回类型:

public ActionResult FileDownload()
{
    string filePth = @"file://file-server/data-folder/pdf/19450205.pdf";
    return File(filePth , "application/pdf");
}

但上面的代码给出了不支持 URI 的异常。

我还尝试使用 FileStream 来读取数组中的字节返回该字节以进行下载,但 FileStream 也给出了不正确的“虚拟路径”错误,因为文件没有放在虚拟路径中,它位于单独的服务器上。

【问题讨论】:

    标签: c# asp.net-mvc


    【解决方案1】:
    public ActionResult Download()
    {
        var document = = @"file://file-server/data-folder/pdf/19450205.pdf";
        var cd = new System.Net.Mime.ContentDisposition
        {
            // for example foo.bak
            FileName = document.FileName, 
    
            // always prompt the user for downloading, set to true if you want 
            // the browser to try to show the file inline
            Inline = false, 
        };
        Response.AppendHeader("Content-Disposition", cd.ToString());
        return File(document.Data, document.ContentType);
    }
    

    【讨论】:

      【解决方案2】:

      感谢您的回复,但两个建议都没有奏效。

      由于需要通过 URI 访问文件,使用 FileInfo 会报错:不支持 URI 格式。

      我设法通过以下机制完成了这项工作:

          public ActionResult FaxFileDownload()
          {
              string filePth = @"file://file-server/data-folder/pdf/19450205.pdf";
      
              WebClient wc = new WebClient();
              Stream s =  wc.OpenRead(filePth);
      
              return File(s, "application/pdf");
      
          }
      

      谢谢大家。

      【讨论】:

        猜你喜欢
        • 2014-08-25
        • 1970-01-01
        • 1970-01-01
        • 2018-04-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多