Question

In my FCGI app I want to make server-side response in a way to make the browser (want to use the majority of them) open "Save as" dialog box and actually save the file at user hard drive. I fail w/ this( Here is the dump of request/response received from Chrome:

Remote Address:192.168.1.69:80  
Request URL:http://192.168.1.69/sunprint/sunweb.fcgi?GETPCBSDATAASFILE2SAVE  
Request Method:GET  
Status Code:200 OK

Request Headers  
GET /sunprint/sunweb.fcgi?GETPCBSDATAASFILE2SAVE HTTP/1.1  
Host: 192.168.1.69  
Connection: keep-alive  
Accept: */*  
X-Requested-With: XMLHttpRequest  
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)   >Chrome/34.0.1847.116 Safari/537.36
Referer: http://192.168.1.69/sunprint/PCBsVersions.html  
Accept-Encoding: gzip,deflate,sdch  
Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4  

Query String Parameters  
GETPCBSDATAASFILE2SAVE  

Response Headers  
HTTP/1.1 200 OK  
Date: Mon, 05 May 2014 10:21:23 GMT  
Server: Apache/2.2.22 (Ubuntu)  
Cache-Control: no-cache, must-revalidate  
Expires: Mon, 26 Jul 1997 05:00:00 GMT  
Content-Description: File Transfer  
Content-Disposition: attachment; filename="SunSerialNumbers.txt"  
Vary: Accept-Encoding  
Content-Encoding: gzip  
Content-Length: 227  
Keep-Alive: timeout=5, max=81  
Connection: Keep-Alive  
Content-Type: text/plain  

The content of my file is some set of printable ASCII symbols. BTW, should I encode the content in some way?

It seems that all the needed headers are present but browser still refuses to show the desired dialog box. Is the wrong combination of headers present?

To make a request i use the following code:

function sendCommandGetFile(url1) {
$.ajax({
url: url1,
type: "GET"
});
}

sendCommandGetFile("sunweb.fcgi?GETPCBSDATAASFILE2SAVE", function(data){});

Thanks a lot for help.

Was it helpful?

Solution

X-Requested-With: XMLHttpRequest

No set of HTTP headers is going to cause the browser to download a file it gets in response to a request using XHR.

You have three basic options.

  1. Don't use XHR in the first place
  2. Store the file data somewhere, give it a temporary URI, pass the URI back in the response, have the client side JS set location to that URI
  3. Construct a data: scheme URI and have the client side JS assign it to location.

Unless you really need to sometimes return a file, and sometimes return data for JS to process (e.g. error messages) then option 1 is the best.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top