我在这个链接上阅读了Luchian的建议
Download Excel file via AJAX MVC
我非常喜欢他的想法,这样我可以避免潜在地保存2份电子表格。 或者不必担心在AJAX调用返回之前删除在服务器上保存的初始文件。
因此,如果我将电子表格保存为内存流并将其插入一个唯一的Cache变量,例如cache_uniq1并在AJAX调用中返回,那么我将如何打开该流?
我可以做window.open吗?
$.ajax({
type: 'POST',
url: '/Reports/Somepage.aspx',
data: '{ "dataprop1": "test", "dataprop2" : "test2" }',
//contentType: 'application/json; charset=utf-8',
//dataType: 'json',
success: function (returnValue) {
//window.location = /Reports/Download?file=' + returnValue;
//
//NOTE: Instead the returnValue = cache_uniq1 which is the unique Cache
//variable name that holds the Excel spreadsheet as a memory stream
}
});
非常感谢任何帮助。仅供参考我的应用程序是直接在ASP.Net网站上(不使用MVC)。
答案 0 :(得分:0)
[OutputCacheAttribute(VaryByParam = "*", Duration = 0, NoStore = true)]
[Authorize]
public ActionResult DownloadExportedFile(string filename = null)
{
if (!String.IsNullOrEmpty(filename))
{
var tempDirPath = System.IO.Path.GetTempPath();
if (System.IO.File.Exists(tempDirPath + filename))
{
var bytes = System.IO.File.ReadAllBytes(tempDirPath + filename);
System.IO.File.Delete(tempDirPath + filename);
var cd = new System.Net.Mime.ContentDisposition
{
FileName = Path.GetFileName("BookingForm.csv"),
// always prompt the user for downloading, set to true if you want
// the browser to try to show the file inline
Inline = false
};
//Response.AppendHeader("Content-Disposition", cd.ToString());
return File(bytes, "text/csv", "BookingForm.csv");
}
}
return new HttpStatusCodeResult(404, "File not found!");
}