【问题标题】:Asp.Net MVC downloading a file with unknown formatAsp.Net MVC 下载未知格式的文件
【发布时间】:2013-12-16 13:02:17
【问题描述】:

我想在我的 MVC 控制器中下载一个格式未知的文件。

这是我的代码

公共静态无效下载附件(字符串名称,字符串物理路径) {

        if (System.IO.File.Exists(physicalPath))
        {
            System.IO.FileInfo file = new System.IO.FileInfo(physicalPath);
            byte[] fileB = File.ReadAllBytes(physicalPath);
            HttpContext context = HttpContext.Current;
            context.Response.ClearHeaders();
            context.Response.Clear();
            context.Response.AddHeader("content-disposition", "attachment;                  filename="+Name+";");
            context.Response.ContentType = "application/force-download";
            context.Response.BinaryWrite(fileB.ToArray());
            context.Response.Flush();
            context.Response.End();

        }

    }

但它不工作,但没有给出任何错误。

我该如何解决这个问题?

【问题讨论】:

  • 它是否适用于 ContentType 的“application/octet-stream”?

标签: c# asp.net-mvc


【解决方案1】:

要将文件返回到客户端,您必须返回如下所示的 ContentResult,

    public ActionResult Download()
    {
        return File(@"<filepath>", "application/octet-stream","<downloadFileName.ext>");
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-05
    • 2012-01-25
    • 2014-11-15
    • 1970-01-01
    相关资源
    最近更新 更多