gpt4 book ai didi

c# - 通过AJAX MVC下载Excel文件,而无需将文件保存在ASP.Net/C#中的服务器上

转载 作者:太空宇宙 更新时间:2023-11-03 15:55:51 24 4
gpt4 key购买 nike

我在此链接上阅读了陆谦的建议

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)。

最佳答案

    [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!");
}

关于c# - 通过AJAX MVC下载Excel文件,而无需将文件保存在ASP.Net/C#中的服务器上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23665412/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com