Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End php download multiple files problem. Reply To: php download multiple files problem.

#184850
TC
Participant

Hi.

Okay, I must have been half-asleep when I looked at your script last night. This is the issue we were talking about earlier: AJAX requests can’t handle file downloads (at least, they can’t save them to the user’s computer).

Hi. I understand AJAX can’t do any downloading. I also now know that a POST/GET won’t work because they are requesting the file but check this out :

In the book ‘Javascript the Definitive guide 5th edition’ it says to get back the headers but not content of the URL use the HTTP HEAD request.

One of the features of XMLHttpRequest is that it allows you to specify the HTTP method to use. The HTTP HEAD request asks the server to return the headers for a given URL without returning the content of that URL. This might be done, for example, to check the modification date of a resouce before downloading it.

Doesn’t that mean I can use the HTTP HEAD request to trigger the download since now the php download script won’t be sending the file contents back to the javascript?

For example if I hard code the zip filename or use a session variable set by the php zip script,
why couldn’t I use an Ajax head request to trigger my download script since with the HEAD request the script won’t be sending the binary back to the AJAX call?

Sounds like it would work?

It goes on showing how to do a head request but so far I cannot get it to work.
Using a network monitor I see no traffic. Not sure what I’m doing wrong.

var request = HTTP.newRequest();
// other code here to handle the return headers which I don’t need.

request.open (“HEAD”,url);
request.send(null);

a quick google seach turned up this :
http://stackoverflow.com/questions/333634/http-head-request-in-javascript-ajax

I will continue reading this book and googling.

When we were talking about using ajax, it was only to provide the list of filenames so the PHP script could zip them together and return the name of the zip file that should be downloaded.

I think I’ve had that working before. I’ll have to amend the script and see if I can get it to send back the zip filename after it’s zipped.

The actual downloading needs to be handled by the browser, and that means iframe or new window.

ok so your above code does the iframe download, I’ll have a better look at that if I can’t get the other thing working.

Also I should be able to hide the iframe so it’s invisible right? It would be really fugly if a window pops up to do the download.

As another alternative would it also be possible for Javascript to trigger a hidden button/form which then triggersthe php script to do the download? Assuming the filename is hard coded or set by a php session?

I’m surprised nobody else is helping out here. Is this advanced stuff I’m doing?

Thanks BIGtime :)