通过AJAX MVC下载Excel文件,而不用在ASP.Net/C#服务器上保存文件
我阅读了陆build在这个链接上的build议
通过AJAX MVC下载Excel文件
我非常喜欢他的想法,我可以避免潜在的保存电子表格的两个副本。 或者不必担心在AJAX调用返回之前删除保存在服务器上的初始文件。
因此,如果我将电子表格保存为内存stream,并将其插入到唯一的cachingvariablescache_uniq1中并在AJAX调用中返回该variables,那么我将如何打开该stream?
我可以做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 Web站点(不使用MVC)。
[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!"); }