我找到了这个链接。ASP.NET MVC download image rather than display in browser
我尝试了所有方法,我的action方法将文件转储到视图中,而不是下载。我希望能够单独下载该文件。谢谢
if (extension.ToLower() == ".docx")
{ // Handle *.jpg and
contentType = "application/docx";
}
else if (extension.ToLower() == ".jpg")
{// Handle *.gif
contentType = "image/jpeg jpeg jpg jpe";
}
else if (extension.ToLower() == ".pdf")
{// Handle *.pdf
contentType = "application/pdf";
}
Response.ContentType = contentType; // "application/force-download";
Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName);
Response.WriteFile(FilePath );
Response.Flush();
Response.End();[![View after clicking download button][1]][1]发布于 2016-10-13 09:31:46
您的问题可能与以下问题类似:
How can I present a file for download from an MVC controller?
Returning a file to View/Download in ASP.NET MVC
您可以使用以下代码从操作方法返回FileResult或FileStreamResult,而不是使用Response.WriteFile:
if (extension.ToLower() == ".docx")
{ // Handle *.jpg and
contentType = "application/docx";
}
else if (extension.ToLower() == ".jpg")
{// Handle *.gif
contentType = "image/jpeg jpeg jpg jpe";
}
else if (extension.ToLower() == ".pdf")
{// Handle *.pdf
contentType = "application/pdf";
}
Response.ContentType = contentType; // "application/force-download";
Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName);
// returning the file for download as FileResult
// third input parameter is optional
return File(FileName, contentType, Server.UrlEncode(Filename));您可能希望这样尝试:
Response.Headers.Add("Content-Disposition", "attachment; filename=" + FileName);其他参考资料:
https://stackoverflow.com/questions/40007286
复制相似问题