【问题标题】:Returning a file using asp.net mvc?使用 asp.net mvc 返回文件?
【发布时间】:2011-03-29 04:14:47
【问题描述】:

我有一个自定义的 IHttpHandler,胖客户端使用它来使用诸如

之类的 url 下载文件
http://url.ashx?id=123&version=456 

代码处理程序基本上以

结尾
context.Response.WriteFile(myLocalServerPath);

是否可以使用典型的 asp.net mvc 控制器模式来替换它?

【问题讨论】:

    标签: c# .net asp.net asp.net-mvc httphandler


    【解决方案1】:

    在一个动作中:

    byte[] fileBytes = ...;
    string fileName = "example";
    return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
    

    (或者更具体的 MIME 类型,如果已知的话)

    【讨论】:

    • 如果我们返回一个更具体的 MIME,它只会显示在浏览器中(如果是图像),我们如何下载它(通过使用更具体的 MIME)?
    【解决方案2】:

    来自这个site,但简化了:

        public FileResult Download()
        {
            string filename = "test.pdf";
            string contentType = "application/pdf";
            //Parameters to file are
            //1. The File Path on the File Server
            //2. The content type MIME type
            //3. The parameter for the file save by the browser
            return File(filename, contentType,"Report.pdf");
        }
    

    【讨论】:

      猜你喜欢
      • 2013-06-21
      • 1970-01-01
      • 1970-01-01
      • 2015-02-08
      • 2011-07-01
      • 2010-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多