我目前使用project awesome來彈出一個表格,要求輸入一個帳號。MVC CSV文件下載+項目真棒
我拿號,並生成一個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文件,然後將文件傳遞給成功函數並使用window.open(url),或者可以使用javascript從json結果重新創建文件?
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,4 4,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」}
我使用彈出窗體的原因是因爲用戶需要輸入一個帳號,然後才能生成csv。賬號構成了account_ref字段的一部分。在用戶輸入帳號之前,帳號是未知的。 – uk101man 2011-06-06 10:11:30
@ uk101man,您的問題並非來自您使用彈出窗體的事實。它來自於您使用AJAX提交此表單的事實。你不應該這樣做。使用常規的'@ Html.BeginForm()'助手來生成表單,並且不要使用任何AJAX來發布它。 – 2011-06-06 10:12:53
,但ajax表單是我相信的項目真棒使用。當我實現你的想法時,csv文件的內容會返回到彈出窗口。 – uk101man 2011-06-06 10:58:10