我想在我的MVC控制器中下载一个格式未知的文件。
这是我的代码
public static void DownloadAttachment(string Name,string physicalPath) {
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();
}
}
但它不起作用但不会出现任何错误。
如何解决这个问题?
答案 0 :(得分:2)
要将文件返回到客户端,您必须返回如下所示的ContentResult,
public ActionResult Download()
{
return File(@"<filepath>", "application/octet-stream","<downloadFileName.ext>");
}