Reputation: 2446
I'm trying to prompt a save as dialog when downloading a file, but what I get is, or the file is opening on a browser or it is downloaded without prompting for the save location and save name.
My Controller's code:
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");
}
I've tried a different variations of FileResult/ActionResult, application/text etc.
Client code:
<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>
Upvotes: 3
Views: 4991
Reputation: 8792
Original answer
You need to set the Content-Disposition
header to attachment
in the response to instruct the browser to save the file.
New answer
It looks like this is a known Chrome issue: https://code.google.com/p/chromium/issues/detail?id=380652
Upvotes: 1