2

I want to open a file (format could vary between things like .docx, .pdf and .jpg) in a new window when someone clicks on a button. However, the URL where the file is located can return either the file, when I use the header {"Accept" : "application/octet-stream"}, or it can return a JSON file with data when using the header {"Accept" : "application/json"}.

When I do

window.open(myUrl);

I cannot specify if I get my octet-stream or json. How can I add my header, or otherwise ensure that I get the proper file?

Daniël Camps
  • 1,737
  • 1
  • 22
  • 33
  • Assuming you do an ajax request when the user clicks the button, I think you should open the new window as part of the ajax callback. This way you specify the header in the request, and open the new window when you receive the response, which should have the content (i.e. the file) you want to show. – francesco foresti May 27 '15 at 12:09
  • possible duplicate of [window.open with headers](http://stackoverflow.com/questions/4325968/window-open-with-headers) – Devin H. May 27 '15 at 12:14
  • @DevinH. I saw that post, that's why I described my problem with details – Daniël Camps May 27 '15 at 12:24
  • The reason I marked as duplicate is because the answer to that question is the answer to this. You would need to use AJAX (where you have control of headers) to receive the data, and then open the new window with the data. In the comments: http://www.w3.org/TR/XMLHttpRequest/#the-setrequestheader%28%29-method. There's an example of AJAX headers there. – Devin H. May 27 '15 at 12:34

0 Answers0