1
我試圖在下載文件時提示另存爲對話框,但是我得到的是或者文件在瀏覽器上打開,或者在沒有提示保存的情況下下載它位置並保存名稱。如何在文件下載時提示「另存爲」對話框
我的控制器代碼:
public FileContentResult Save(string text)
{
string contentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment; filename=outname.txt"); //EDIT
return File(Encoding.ASCII.GetBytes(text), contentType, "outname.txt");
}
我試過FileResult/ActionResult的,應用程序/文本等的不同變化
客戶端代碼:
<html>
<body>
<script>
function submitForm() {
txt = document.getElementById("textFld");
form = document.getElementById("submitForm");
input = document.getElementById("messages").outerHTML;
txt.value = input;
form.submit();
};
</script>
<table id="messages"> ... </table>
<form action="Home/Save" method="POST" id="submitForm">
<input type="text" name="text" id="textFld">
</form>
<input type="button" id="submitBtn" onclick="submitForm()">
<script>
subm = document.getElementById("submitBtn");
subm.click();
</script>
</body>
</html>
我已經試過這樣:Response.AppendHeader( 「內容處置」,「附件;文件名= out.out 「);但它不起作用...... – Michael 2015-04-04 13:18:07
你真的在瀏覽器接收到的響應中看到該標題嗎? – arootbeer 2015-04-04 13:25:39
是:.... 內容類型:application/octet-stream ... 內容處置:附件; filename = out.out ...... – Michael 2015-04-04 13:28:49