Response.WriteFile()-无法使用ASP.NET MVC 4.5 [英] Response.WriteFile() -- not working asp net mvc 4.5
问题描述
我查看了很多资源,以下内容应该可以使用,但是我的另存为"对话框从未显示(不是特定于浏览器的):
I've looked at many resources and the following should work, however my save as dialog box is never showing (not browser specific):
Response.ContentType = "application/octet-stream";
string downloadName = "Request "+request.RequestID+ "_researchExport.doc";
Response.AddHeader("Content-Length",
new System.IO.FileInfo(FileName).Length.ToString());
Response.AddHeader("Content-Disposition",
string.Format("attachment; filename={0};", downloadName));
Response.WriteFile(FileName);
Response.Flush();
Response.End();
该文件肯定存在.我还尝试了以下方法:
The file definitely exists. I've also tried the following:
-
改为使用
Response.TransmitFile(FileName)
省略了Response.End()
省略了Content-Length
标头
使用this.ControllerContext.HttpContext.Response
任何帮助都将得到高度的感谢
Any help would be greatly appreciated
推荐答案
解决了该问题.我从一个视图中使用Ajax调用此函数.这没有用(我不确定为什么),但是我认为可能是因为我有多个控制器.从@HTML.ActionLink()
调用此命令可以正确显示提示.
Solved the issue. I was using Ajax to call this function from one of my views. This didn't work (I am not sure why), but I think it may be because I have multiple controllers. Calling this from @HTML.ActionLink()
had the prompt display properly.
这篇关于Response.WriteFile()-无法使用ASP.NET MVC 4.5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!