Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Asp.Net MVC正在下载格式未知的文件_C#_Asp.net Mvc - Fatal编程技术网

C# Asp.Net MVC正在下载格式未知的文件

C# Asp.Net MVC正在下载格式未知的文件,c#,asp.net-mvc,C#,Asp.net Mvc,我想在我的MVC控制器中下载一个未知格式的文件 这是我的代码 公共静态void下载附件(字符串名称、字符串物理路径) { if (System.IO.File.Exists(physicalPath)) { System.IO.FileInfo file = new System.IO.FileInfo(physicalPath); byte[] fileB = File.ReadAllBytes(physicalP

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

这是我的代码

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

        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();

        }

    }
但它不工作,但不会给出任何错误


如何解决此问题?

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

    public ActionResult Download()
    {
        return File(@"<filepath>", "application/octet-stream","<downloadFileName.ext>");
    }
public ActionResult下载()
{
返回文件(@“,”应用程序/八位字节流“,”);
}

它是否适用于“应用程序/八位字节流”的ContentType?