0

I am trying to download a CSV file to the browser. I am unable to get it to properly download the file in IE11. Instead it opens a new tab and write the CSV contents to the tab.

I am using just the following javascript:

window.open(sUrl, '_target');

In the IE Developer tools, I can see the Response Header shows:

Content-Type: text/csv; charset=UTF-8

I would like it to prompt the user to save the file. I've changed the browser settings to always download, yet it still doesn't do it.

I would like to fix this from a change to the Javascript, if possible. How can I do that?

George Hernando
  • 2,550
  • 7
  • 41
  • 61

1 Answers1

0

As far as I know, it's not possible to do this with JavaScript. Take a look at this answer if you're comforable using PHP to download the file.

According to that answer, your PHP should look something like this:

<?php
    $file_url = 'http://www.myremoteserver.com/file.csv';
    header('Content-Type: text/csv; charset=UTF-8');
    header("Content-Transfer-Encoding: Quoted-Printable"); 
    header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\""); 
    readfile($file_url);
?>
v3ryn3rdy
  • 21
  • 1
  • 5
  • Thanks. Unfortunately I am not using PHP and don't have much control over the header response from the server. – George Hernando May 27 '18 at 01:30
  • @GeorgeHernando Sorry about that. Best of luck! – v3ryn3rdy May 27 '18 at 15:16
  • 1
    Hey @GeorgeHernando [Take a look at this](https://ourcodeworld.com/articles/read/189/how-to-create-a-file-and-generate-a-download-with-javascript-in-the-browser-without-a-server). Worked for me. You might want to get the value with an `XMLHttpRequest` and then download it with the method in the linked article. Good luck! – v3ryn3rdy May 27 '18 at 20:05
  • That's interesting. I didn't know that you could do that. – George Hernando May 30 '18 at 18:44