2

So this post gives an answer to how to send a post for downloading a file using forms. But my API endpoint isn't recognizing the form data.

[HttpPost]
public async Task<IHttpActionResult> DownloadPdfs([FromBody] IEnumerable<int> ids)
{
    if (ids == null || ids.Count() < 1)
    {
        return BadRequest("No ids supplied.");
    }
    ...

'ids' always has a count of 0.


Here's the JS code:

function downloadFile(ids) {    
  var win = 'w' + Math.floor(Math.random() * 1000000000000);
  window.open('', win, 'width=250, height=100');
  var f = $('<form></form>')
      .attr({ target: win, method: 'post', action: 'api/pdfs' })
      .appendTo(document.body);
  _.each(ids, function (id) {
      $('<input></input>')
          .attr({ type: 'hidden', name: 'ids', value: id })
          .appendTo(f);
  });
  f[0].submit();
  f.remove();
}

Initially, I just had it the same as the other linked answer which was just one input appended to the form, in which I set the value to 'ids'. I tried this next, but that still didn't work.

Does anyone know how to adapt this to still use post data to provide my ids, which are needed to create and supply the downloaded file? Or is there a better way to do this?

I've tried doing it just using Ajax, which returns the file content in the responseText, but I can't make use of it in there and I can't get the browser to open it.

Edit 1

  • I also just tried setting the value to this:
    value: '{ids: [' + ids.toString() + ']}'
    That didn't work.

Edit 2

  • Just tried changing the endpoint parameter to this:
    ([FromBody] int[] ids)
    ...to no avail.
14
  • 1
    and where is your js code? Commented Dec 27, 2017 at 22:59
  • @MarceloOrigoni Sorry, should have put that in off the bat. Commented Dec 27, 2017 at 23:09
  • @dferenc I actually just modified the title. I realized that was not very clear about the actual problem. But apparently yes, I can't get the data to make it into the endpoint. Commented Dec 27, 2017 at 23:45
  • @DanielEcker Thanks for clarifying! Removing my other comments now. Commented Dec 27, 2017 at 23:50
  • 1
    I think this is your answer: stackoverflow.com/questions/26324529/… Commented Dec 28, 2017 at 17:08

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.