1

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?

h bob
  • 3,610
  • 3
  • 35
  • 51
  • 1
    Can you post the html? specifically the `form` tag. – ramiramilu Jun 02 '15 at 07:37
  • @ramiramilu added markup – h bob Jun 02 '15 at 07:42
  • @hbob: why are you redirecting after submitting form. remove this line `window.location.href = $(this).attr("action");` this line will call GET method not the post. – Amit Kumar Jun 02 '15 at 07:44
  • 1
    I replicated your code in my local, and I was not able to get second time hit. There may be other parts of the code which is causing the problem. – ramiramilu Jun 02 '15 at 07:45
  • @AmitKumar That line is to ensure the page does not change. It asks "do you want to open/save" but the page remains as is. It is standard practice as far as I can tell. – h bob Jun 02 '15 at 07:46
  • Try adding event.preventDefault. – Tom D'Hulster Jun 02 '15 at 07:46
  • @ramiramilu Thanks for doing that! That confirms the second request is NOT normal. PS, which browser did you test in? – h bob Jun 02 '15 at 07:47
  • 1
    chrome Version 43.0.2357.81 – ramiramilu Jun 02 '15 at 07:47
  • @ramiramilu AHA! I just tested in more browsers, and I only get this problem in Firefox! I wonder what it could be. I guess it's either standard for Firefox, or due to some plugin. – h bob Jun 02 '15 at 07:49
  • 1
    in fact in Firefox, I tried and i am not able to reproduce your issue. – ramiramilu Jun 02 '15 at 07:50
  • @ramiramilu Post something as an answer, so I can accept it! :-) – h bob Jun 02 '15 at 07:50
  • @ramiramilu So then it must be a Firefox plugin I'm using. In which case, I can probably safely ignore this problem, even though I can't find the cause. – h bob Jun 02 '15 at 07:50
  • 1
    Can you disable all the plugins in FF and give a try and let me know, then I can post the solution as answer :-) – ramiramilu Jun 02 '15 at 07:51
  • @ramiramilu Good idea, now I just need to find out how to do that... give me a bit of time... OK turns out it's easy, just "Help" then "Restart with Addons disabled" – h bob Jun 02 '15 at 07:53
  • @ramiramilu YES that worked! So it's definitely some naughty plugin. Please add your anwer, thanks for your help. – h bob Jun 02 '15 at 07:58

2 Answers2

3

I do not see anything wrong with your code. I replicated your code and tried to reproduce the issue which you are facing. But I am not able to repro in Chrome, IE and Firefox.

It looks like this issue is specific to FireFox browser of yours and due to some plugin/Extension. Disable all the plugins in the browser and give it a try, it should work.

ramiramilu
  • 17,044
  • 6
  • 49
  • 66
0

You are missing with e.preventDefault() in your script. What is happening here is your action is hit by both script and normal post. So if you put e.preventDefault() like below, then it will prevent browser's default postback and only javascript's code will execute your request.

$("#myForm").on("submit", function(e) {
  e.preventDefault();
  window.location.href = $(this).attr("action");
});
Ankush Jain
  • 5,654
  • 4
  • 32
  • 57
  • Not true. In a jQuery handler, `return false` means 1) `preventDefault()`, 2) `stopPropagation()` and 3) returns false immediately. [See here](https://stackoverflow.com/questions/1357118/event-preventdefault-vs-return-false). – h bob Jun 02 '15 at 09:06