javascript - How to get binary data from server using JQuery? -


I am trying to download binary data using a web API controller.

Code I has been tried:

  $ Get ("../ api / download / report? Id =" + selectItem.Id);  

which replaces this URL with an HTTP GET when I debug the running app:

  http: // localhost: 12150 / api / Download / report? ID = 2515  

The browser displays 200 results, returns binary data, but it does not open it or offer to download the file. If I copy and paste the URL in the address bar and press enter, it works.

Any suggestions or alternatives that allow relative paths?

You should not use AJAX for these types of things. If you want to force the user to download the response, then it can be dirty. Open a new tab / window instead; This will stop anyway in most browsers, and the user will not even see it was open! :) If you know in your URL on load, you can easily create an anchor tag with target = "_ blank" -attribute. I.e .:

  & lt; A href = "http: // localhost: 12150 / api / download / report? Id = & lt;? = $ ReportID? & Gt;" Target = "_ blank" & gt; Download report & lt; / A & gt;  

(Well, you know basic HTML, but you get this idea)

If you want to do this with JS, then a simple Window.open () :

  window.open ("../ api / download / report? Id =" + selected id. Id, '_ blank') ;  

Disclaimer : I do not know that this relative URL will actually work in window.open () . If this does not fool the URL for the first time with the creation of the URL:

  $ reportURL = $ _SERVER ['SERVER_NAME']. "/ API / Download / Report? Id =";  

And then, in your JS, just resize it:

  window.open ("& lt;? = $ ReportURL?", + + Selected item id, '_ blank');  

Comments

Popular posts from this blog

winforms - C# Form - Property Change -

javascript - amcharts makechart not working -

java - Algorithm negotiation fail SSH in Jenkins -