Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #17444 > unrolled thread

Ajax Call to PHP Download.php Makes Browser Not Download, But Client Still Receives Stream

Started bydman777 <darin.hensley@gmail.com>
First post2012-12-03 16:14 -0800
Last post2012-12-04 00:48 +0000
Articles 3 — 2 participants

Back to article view | Back to comp.lang.javascript


Contents

  Ajax Call to PHP Download.php Makes Browser Not Download, But Client Still Receives Stream dman777 <darin.hensley@gmail.com> - 2012-12-03 16:14 -0800
    Re: Ajax Call to PHP Download.php Makes Browser Not Download, But Client Still Receives Stream dman777 <darin.hensley@gmail.com> - 2012-12-03 16:16 -0800
      Re: Ajax Call to PHP Download.php Makes Browser Not Download, But Client Still Receives Stream Denis McMahon <denismfmcmahon@gmail.com> - 2012-12-04 00:48 +0000

#17444 — Ajax Call to PHP Download.php Makes Browser Not Download, But Client Still Receives Stream

Fromdman777 <darin.hensley@gmail.com>
Date2012-12-03 16:14 -0800
SubjectAjax Call to PHP Download.php Makes Browser Not Download, But Client Still Receives Stream
Message-ID<74b314b0-3138-4fdf-b31e-bc7c9b1dbcba@googlegroups.com>
This script is for my http file server. I have my files out of web root, so I am using a php script to grab and send the files to the client. The problem is, when I click on the file to download the browser(ff and chrome) does not ask me if I want to save the file. In chrome's web tools, under network, I see that the download.php did execute successfully. In fact, I even see where the file was transfered in bytes for the request. It seems like the browser is not downloading the file, but the file is being sent to the browser and cached.

Web server is Nginx.
This goes for any type of file. I tried text file, mp3, and zip.

I must emphasis that THE FILE IS BEING SENT TO THE BROWSER. I verified it in chromes network tab for bytes transferred. But for some reason, the file is not being saved on the client computer,the browser does ask to save file, and the browser does not have a record of the downloaded file.

I have verified this script works on other web servers. I also verified other another http file server nginx user does not have this issue(using php). So, therefore I think it maybe because I am calling it with ajax.

    <?php
    
    $path = "/trunk/";
    $file = $_POST['filename'];
    $file_ext = pathinfo($file, PATHINFO_EXTENSION);
    $file_name = pathinfo($file, PATHINFO_BASENAME);
    $file_full = $path . $file_name;
    
    $finfo = new finfo(FILEINFO_MIME_TYPE);
    
    $ctype = $finfo -> file($file_full);
    header("Content-Disposition: attachment; filename=\"".$file_name."\"");
    header("Content-Type: " .$ctype);
    header("Content-Length: " .filesize($file_full));
    error_log(filesize($file_full));
    
    readfile($file_full);
    
    ?>

Request Header:

    POST /php/download.php HTTP/1.1
    
        Host: www.example.com
        Connection: keep-alive
        Content-Length: 146
        Origin: https://www.example.com
        User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.79 Safari/537.4
        Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryMA7u5AkYPL9qGmRY
        Accept: */*
        Referer: https://www.example.com/index.php
        Accept-Encoding: gzip,deflate,sdch
        Accept-Language: en-US,en;q=0.8
        Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

RESPONSE HEADER

    HTTP/1.1 200 OK
    Server: nginx
    Date: Sat, 01 Dec 2012 20:22:28 GMT
    Content-Type: application/zip
    Content-Length: 75090
    Connection: keep-alive
    Keep-Alive: timeout=300
    X-Powered-By: PHP/5.4.6--pl0-gentoo
    Content-Disposition: attachment; filename="try.zip"
    Expires: Sat, 01 Dec 2012 20:22:27 GMT
    Cache-Control: no-cache

[toc] | [next] | [standalone]


#17445

Fromdman777 <darin.hensley@gmail.com>
Date2012-12-03 16:16 -0800
Message-ID<6e5480bd-042f-4ec3-9d98-a00f64089423@googlegroups.com>
In reply to#17444
On Monday, December 3, 2012 6:14:12 PM UTC-6, dman777 wrote:
> This script is for my http file server. I have my files out of web root, so I am using a php script to grab and send the files to the client. The problem is, when I click on the file to download the browser(ff and chrome) does not ask me if I want to save the file. In chrome's web tools, under network, I see that the download.php did execute successfully. In fact, I even see where the file was transfered in bytes for the request. It seems like the browser is not downloading the file, but the file is being sent to the browser and cached.
> 
> 
> 
> Web server is Nginx.
> 
> This goes for any type of file. I tried text file, mp3, and zip.
> 
> 
> 
> I must emphasis that THE FILE IS BEING SENT TO THE BROWSER. I verified it in chromes network tab for bytes transferred. But for some reason, the file is not being saved on the client computer,the browser does ask to save file, and the browser does not have a record of the downloaded file.
> 
> 
> 
> I have verified this script works on other web servers. I also verified other another http file server nginx user does not have this issue(using php). So, therefore I think it maybe because I am calling it with ajax.
> 
> 
> 
>     <?php
> 
>     
> 
>     $path = "/trunk/";
> 
>     $file = $_POST['filename'];
> 
>     $file_ext = pathinfo($file, PATHINFO_EXTENSION);
> 
>     $file_name = pathinfo($file, PATHINFO_BASENAME);
> 
>     $file_full = $path . $file_name;
> 
>     
> 
>     $finfo = new finfo(FILEINFO_MIME_TYPE);
> 
>     
> 
>     $ctype = $finfo -> file($file_full);
> 
>     header("Content-Disposition: attachment; filename=\"".$file_name."\"");
> 
>     header("Content-Type: " .$ctype);
> 
>     header("Content-Length: " .filesize($file_full));
> 
>     error_log(filesize($file_full));
> 
>     
> 
>     readfile($file_full);
> 
>     
> 
>     ?>
> 
> 
> 
> Request Header:
> 
> 
> 
>     POST /php/download.php HTTP/1.1
> 
>     
> 
>         Host: www.example.com
> 
>         Connection: keep-alive
> 
>         Content-Length: 146
> 
>         Origin: https://www.example.com
> 
>         User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.79 Safari/537.4
> 
>         Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryMA7u5AkYPL9qGmRY
> 
>         Accept: */*
> 
>         Referer: https://www.example.com/index.php
> 
>         Accept-Encoding: gzip,deflate,sdch
> 
>         Accept-Language: en-US,en;q=0.8
> 
>         Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
> 
> 
> 
> RESPONSE HEADER
> 
> 
> 
>     HTTP/1.1 200 OK
> 
>     Server: nginx
> 
>     Date: Sat, 01 Dec 2012 20:22:28 GMT
> 
>     Content-Type: application/zip
> 
>     Content-Length: 75090
> 
>     Connection: keep-alive
> 
>     Keep-Alive: timeout=300
> 
>     X-Powered-By: PHP/5.4.6--pl0-gentoo
> 
>     Content-Disposition: attachment; filename="try.zip"
> 
>     Expires: Sat, 01 Dec 2012 20:22:27 GMT
> 
>     Cache-Control: no-cache

Sorry, here is the javascript Ajax call also:

function downloadFile(event) {
	var fname = event.target.getAttribute("href");
	fname = fname.replace("#","");
	var form = new FormData();
	form.append("filename", fname);

	var xhr = new XMLHttpRequest();

	xhr.open("POST", "php/download.php", true);
	xhr.send(form);
}

[toc] | [prev] | [next] | [standalone]


#17448

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2012-12-04 00:48 +0000
Message-ID<k9jh9k$6m2$1@dont-email.me>
In reply to#17445
On Mon, 03 Dec 2012 16:16:14 -0800, dman777 wrote:

>> This script is for my http file server. I have my files out of web
>> root, so I am using a php script to grab and send the files to the
>> client. The problem is, when I click on the file to download the
>> browser(ff and chrome) does not ask me if I want to save the file. In
>> chrome's web tools, under network, I see that the download.php did
>> execute successfully. In fact, I even see where the file was transfered
>> in bytes for the request. It seems like the browser is not downloading
>> the file, but the file is being sent to the browser and cached.

Yes, you're fetching a response inside an ajax event .... so the response 
goes to the ajax code.

If you want to download the response to the local hard disk on the client 
machine, then you should use a normal browser call, not an ajax call to 
launch the request that downloads the file.

Rgds

Denis McMahon

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web