meena
meena

Reputation: 93

Force the browser to download file as CSV from url

I am downloading a CSV file from a URL how do I set certain response headers to download my file

Desired response headers:

HTTP/1.1 200 OK
Date: Mon, 29 May 2017 06:03:38 GMT
Content-Type: application/octet-stream
Content-Length: 340
Connection: keep-alive
Expires: Tue, 03 Jul 2001 06:00:00 GMT
Last-Modified: Mon May 29 11:33:38 IST 2017
Cache-Control: no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0
Pragma: no-cache
Content-Disposition: attachment; filename="5756913MerchantTransactionFile20170529113338.csv"
Server: Some
Access-Control-Allow-Origin: 
Access-Control-Allow-Origin: 
authorized: true
authorizehtml: true

Current response headers

HTTP/1.1 200 OK
Date: Mon, 29 May 2017 06:16:52 GMT
Server: Apache/2.4.18 (Unix) PHP/5.5.36
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE, PUT
Access-Control-Max-Age: 1000
Access-Control-Allow-Headers: x-requested-with, Content-Type, origin, authorization, accept, client-security-token
Expires: Tue, 03 Jul 2001 06:00:00 GMT
Last-Modified: Mon May 29 11:46:52 IST 2017
Cache-Control: no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0
Pragma: no-cache
Content-Type: application/json
Content-Length: 0
Keep-Alive: timeout=5, max=97
Connection: Keep-Alive

My code:

openFile(url) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {

    }
};
xhttp.open("GET",url, true);
xhttp.send();
  //window.open(url, '_blank')
}

Trying to download a csv file from the url. Please help me out. How do we force the browser to download a file.

Upvotes: 4

Views: 3957

Answers (2)

Nadir Laskar
Nadir Laskar

Reputation: 4160

Trick here is to convert your data into blob and simulate a click event on an anchor tag with blob url.

var blob = new Blob([content]);

create an click event

var evnt =  new Event('click');

then create an anchor tag as follows and dispatch event

  $("<a>", {
    download: filename,
    href: webkitURL.createObjectURL(blob)
  }).get(0).dispatchEvent(evnt);

Here is a function

 var download = function(filename, content) {
  var blob = new Blob([content]);
  var evnt =  new Event('click');

  $("<a>", {
    download: filename,
    href: webkitURL.createObjectURL(blob)
  }).get(0).dispatchEvent(evnt);
};

Use the above download function.

USAGE: call the function download with two parameters filename and content

openFile(url) {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function(data) {
    if (this.readyState == 4 && this.status == 200) {
      download('file.csv', data);
    }
  };
  xhttp.open("GET", url, true);
  xhttp.send();
  //window.open(url, '_blank')
}

Upvotes: 2

Oleg M
Oleg M

Reputation: 265

You should specify correct Accept header when making a request. Then server, in response, will put the correct Content-Type header. In your case you should add this line when creating XMLHttpRequest:

xhttp.setRequestHeader("Accept", "text/csv; charset=utf-8");

Upvotes: -1

Related Questions