javascript - Trying to download imagefile through XML request via PHP -



javascript - Trying to download imagefile through XML request via PHP -

been struggling problem while. trying forcefulness file download through javascript , php.

if visit php page straight works want download start when on different page without success.

the url file depends on value taken select item, part works expected leaving out of question.

javascript

var xmlhttp = new xmlhttprequest(); xmlhttp.open("get", posturl, true); //posturl = http://www.example.com/downloadfile.php?url=http://www.example.com/files/test.eps xmlhttp.setrequestheader("content-type", "application/x-www-form-urlencoded"); xmlhttp.send();

php

$url = "http://www.example.com/files/test.eps"; $filesize= strlen(file_get_contents($url)); header('content-description: file transfer'); header('content-type: application/force-download'); header('content-disposition: attachment; filename='.basename($url)); header('expires: 0'); header('cache-control: must-revalidate'); header('pragma: public'); header('content-length: ' . $filesize); readfile($url);

i have been testing both , post (not sure 1 best, sense free right me) trying acquire work. said, if visit "http://www.example.com/downloadfile.php" straight file downloaded , if console.log(xmlhttp.responsetext) file contents written console, file not downloaded when making request other page.

any help appreciated.

turns out used link posted @vivasaayi, did slight twist. instead of using iframe used regular href. php-file unchanged.

javascript

var downloadlinkid = 'hiddendownloadlink', downloadhref = document.getelementbyid(downloadlinkid); if (downloadhref === null) { downloadhref = document.createelement('a'); downloadhref.id = downloadlinkid; downloadhref.setattribute('href', posturl + params); downloadhref.setattribute('onclick', firefoxdownload(posturl + params)); // ff/ie must have onclick event create link clickable downloadhref.click(); } function firefoxdownload(url) { window.location.href = url; //needed ff/ie forcefulness download of file }

javascript php xmlhttprequest

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -