I have an action which serves a file:
public override FileContentResult Foo() {
var someDataAsBytes = ...
return File(someDataAsBytes, "text/csv", "somefilename.csv");
}
The form is simple:
<form method="get" action="/Foo" id="myForm">
<button type="submit">Download</button>
</form>
I initiate the download via script (so the page doesn't change):
$("#myForm").on("submit", function() {
window.location.href = $(this).attr("action");
return false;
});
Problem is that two requests are sent to the server: one GET and one HEAD. And the action runs twice, but only serves the file once.
How do I get it to only send one request? Or is this normal behavior?
Extra info:
I inspected this in Fiddler, and the responses to the GET and POST are identical, except the GET has a non-zero Content-Length
and the actual payload data. The response to the HEAD has nothing. Both return 200 OK status codes. Is this normal?