gpt4 book ai didi

asp.net-mvc-3 - MVC CSV 文件下载 + Project Awesome

转载 作者:行者123 更新时间:2023-12-04 17:12:43 26 4
gpt4 key购买 nike

我目前正在使用 project awesome弹出一个表格,要求输入帐号。

我获取号码​​并生成一个 csv 文件并将其发送到浏览器:

string billcsv = "account_ref,line1,line2,line3"

var VIRFile = File(new System.Text.UTF8Encoding().GetBytes(billcsv), "text/csv", "billfor" + accnumber+ ".csv")

return Json(VIRFile);

我希望最终用户提示保存 csv 文件,但不知道该保存给谁。

我应该先在磁盘上创建 CSV 文件,然后将文件的 url 传递给成功函数并使用 window.open(url) 还是可以使用 javascript 从 json 结果重新创建文件?

杰森结果:

{"FileContents":[65,99,99,111,117,110,116,95,82,69,70,44,73,78,86,79,73,67,69,95,84,89,80,69,44,73,78,86,79,73,67,69,95,82,69,70,44,81,84,89,95,79,82,68,69,82,44,83,69,82,86,73,67,69,95,84,69,88,84,44,85,78,73,84,95,80,82,73,67,69,44,83,69,82,86,73,67,69,95,65,77,79,85,78,84,13,10,114,114,114,44,73,110,118,111,105,99,101,44,86,73,82,49,48,50,44,49,44,83,116,97,114,83,104,105,112,32,32,79,110,101,13,10,44,76,79,65,32,45,32,32,109,116,114,115,13,10,44,71,82,84,32,45,71,84,44,48,44,48,44,48,13,10,114,114,114,44,73,110,118,111,105,99,101,44,86,73,82,49,48,50,44,50,44,66,111,97,116,32,84,114,97,110,115,102,101,114,115,32,72,105,114,101,32,67,104,97,114,103,101,44,50,53,48,46,48,48,44,53,48,48,46,48,48,13,10,114,114,114,44,73,110,118,111,105,99,101,44,86,73,82,49,48,50,44,51,44,66,101,114,116,104,105,110,103,32,32,82,70,65,32,47,32,77,111,68,44,51,53,48,46,48,48,44,49,48,53,48,46,48,48,13,10],"ContentType":"text/csv","FileDownloadName":"billfor123.csv"}

最佳答案

首先不要使用 AJAX 下载文件。使用正常的表单提交或指向将提供文件的 Controller 操作的 anchor 。然后在 Controller 中:

public ActionResult Download(string accnumber)
{
string billcsv = "account_ref,line1,line2,line3";
var data = Encoding.UTF8.GetBytes(billcsv);
string filename = "billfor" + accnumber + ".csv";
return File(data, "text/csv", filename);
}

现在为了调用此操作并提示用户下载,只需创建一个链接:
@Html.ActionLink("Download csv", "Download", new { accnumber = "123" })

或者如果您使用的是表格:
@Html.BeginForm("Download", "SomeController")
{
@Html.TextBox("accnumber")
<input type="submit" value="Download CSV" />
}

关于asp.net-mvc-3 - MVC CSV 文件下载 + Project Awesome,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6250404/

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