从下载文件FileResult不保存到磁盘 [英] Download File from FileResult without saving to disk
问题描述
我使用MVC 4。
我在我的控制器的方法生成一个CSV需求的文件,我想这个文件被用户下载,然后无需在服务器端保存到磁盘。所以,我传递一个MemoryStream文件()对象以避免首先文件保存到磁盘,后来给他下载的路径。
I have a method in my controller which generates a CSV file on demand and I want this file to be then downloaded by the user without the need of saving it to the disk at the server side. So I am passing a MemoryStream on the File() object to avoid having to firstly save file to disk and later give him the path for download.
控制器方法:
[HttpGet]
public ActionResult GenerateCsv(string data)
var sb = new StringBuilder();
sb = GetCsvReportToString(data);
var stream = new MemoryStream(Encoding.UTF8.GetBytes(sb.ToString()));
return this.File(stream, "text/csv", "ReportCsv.csv");
}
SB(StringBuilder)对具有正确的数据,因为我已经调试它,并证实了它。
sb (StringBuilder) has the correct data, as I have debugged it and confirmed it.
我的javascript code:
my javascript code:
window.open(generateFileLink + '?data=' + dataToSend, '_blank');
窗口得到实际上打开,但会出现一个错误的下载对话框,说不能从本地主机下载:
The window gets in fact opened, the download dialog appears but with an error, saying that it cannot be downloaded from localhost:
这个名字是在下载ReportToCsv不同,因为我没有用在我的问题方法的真实姓名,但没有关系。
The name is different in the download "ReportToCsv" because I didn't use real names for methods in my question, but doesn't matter.
任何人都可以帮助?
推荐答案
在这个问题的答案可以在这里找到: HTTP:/ /stackoverflow.com/a/11267754/1417487
The answer to this question can be found here: http://stackoverflow.com/a/11267754/1417487
这是一个关于缓存的头,IE浏览器版本6-8。
It's a regarding the headers of cache, for IE versions 6-8.
这篇关于从下载文件FileResult不保存到磁盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!