martvefun 0 Report post Posted April 18, 2010 Hi !On my website I'ld like to add a link to download a file.The problem is that :- the file is saved with an id and not the full name (stored in a database). I want to change it before sending it (don't know yet how to do that).- I've to check that the user has the right permissions to access this file. function getFile(id) { var xhr_download = getXMLHttpRequest(); xhr_download.onreadystatechange = function() { if (xhr_download.readyState == 4 && (xhr_download.status == 200 || xhr_download.status == 0)) { alert(xhr_download.responseText); $('#loader').hide(); } else if (xhr_download.readyState < 4) { $('#loader').show(); } }; xhr_download.open("GET", "proceed_files.php?action=getfile&id="+id, true); xhr_download.send(null);} <?php// ... try { header("Content-disposition:filename='".$path."'"); header("Content-type:application/octetstream"); echo file_exists($path); } catch (Exception $e) { echo $e->getMessage(); }?>return me "1" but nothing happensif I put this piece of code in a php file and call it directly it worksSo I guess the problem is with the ajax requestThank you Share this post Link to post Share on other sites
Rigaudon 0 Report post Posted April 18, 2010 Header output must be given before any HTML is outputted. If you call the AJAX function, then the HTML is obviously outputted already.The best solution to this would to not use an AJAX function, but rather store that file that lets the user download in another .php file. Then, instead of calling the AJAX function, have it link to that new page with target="_blank". Share this post Link to post Share on other sites
truefusion 3 Report post Posted April 18, 2010 I'm not really sure why this requires AJAX to begin with. But if for some reason it does, then why not just redirect to the file through JavaScript redirects if they are allowed to download the file? Most browsers (if not all) won't cause a blank page to occur but pop up their internal download dialog. And even if some do, hitting back shouldn't cause any loops to occur. Header output must be given before any HTML is outputted. If you call the AJAX function, then the HTML is obviously outputted already.But, at least in theory, the browser should be treating the requested page as any other page (except for the fact that it doesn't render the page to any view unless so desired by the programmer), therefore it should be interpreting the headers as well. I'm quite sure you can set cookies using AJAX (which themselves are done through headers). Share this post Link to post Share on other sites
martvefun 0 Report post Posted April 18, 2010 (edited) ok, it's right I don't really need ajax...I didn't know for the html rulea link like : <a href="proceed_files.php?action=getfile&id=5">download</a>" and the same php file seems to works I stay on the same page so no problems Edited April 18, 2010 by martvefun (see edit history) Share this post Link to post Share on other sites
k_nitin_r 8 Report post Posted May 4, 2010 Hi!@martvefunI've looked at the source code that you have inserted into the post and I see the problem right there!In the PHP code sample, you are echo-ing the return value of the file_exists function and are not returning the actual file itself which is where the problem is. The file_exists function returns a true or a 1, which is what your PHP script is return to the web browser client. If you echo some actual content there, you should be able to obtain the file through your web browser.BTW, you did mention that the file you have is stored within the database and not on the file system. There is a bit of a disagreement among software developers over which a good approach is. For some software developers, the storage of files on the file system implies additional administrative overhead in that the backup schedule has to backup both the data stored within a database and the files stored on the disk. When using a storage area network, the problem is actually a non-issue because the files are stored on a network drive and are backed up anyway by some other utility. Advocates of storing files on the disk suggest that it reduces the load on the database and are in the favor of using less resource intensive methods of storing and retrieving data within the system. The thought about managing files within databases isn't as much of a concern as it used to be because databases can hold huge amounts of data and any operating system limits on the file sizes can easily be worked around because the database stores the logical data within blocks that can be split across files, directories, and even physical disks.The solution that you choose in the end should be something that you can manage and maintain in the long term, so you might want to perform a comparison between storing the files on the disk and storing the files in the database to be able to determine which is a better solution for you. Share this post Link to post Share on other sites