javascript : send file to php script for user to download -


in website i'm building, i'm trying provide users ability export , download data in kml format. i've built php script generate kml file string. works reasonably well, until requests long , 414 errors.

the thing is, i'm pretty sure i'm going @ wrong way (i'm new php). rather sending data string, can multiple tens of thousands of characters long, should sending file generated javascript php script send user or this. possible?

if not, other options have?

here's php script :

if($_server['request_method']=='post') {     $text = $_post['text'];  $text = str_replace('\r\n', php_eol, $text); $text = str_replace('\n', php_eol, $text); $text = str_replace('\t', "\t", $text); $text = str_replace('_hash_', "#", $text);  $filename  = $_post['filename']; $tmpname = tempnam(sys_get_temp_dir(), $filename); $file = fopen($tmpname, 'w');  fwrite($file, $text); fclose($file);  header('content-description: file transfer'); header("content-type: application/octet-stream"); //header('content-type: text/plain'); header('content-disposition: attachment; filename=' . $filename . '.kml'); header('content-transfer-encoding: binary'); header('expires: 0'); header('cache-control: must-revalidate'); header('pragma: public'); header('content-length: ' . filesize($tmpname));  ob_clean(); flush(); readfile($tmpname);  unlink($tmpname); exit; } 

* edit *

ok, changed php script use post instead of should clear length issue. has shown me problem.

i'm using dojo write website. i'm doing xhr request post method. request comes successfully. in success handler, i'm navigating same url passed request setting src of hidden iframe (so avoid reloading page). results in request doesn't work script (post).furthermore, believe means i'm running php script twice makes no sense.

should navigate directly url of php script required parameters? in case, how navigate url using post request?

i'm bit confused of this, shows too!

thanks,

* solution *

mguimard sent me on right track solve issue. here's bit of code used send post request while avoiding realod page:

        download: function(text) {              var ifr = document.createelement('iframe');             ifr.setattribute('name', "target");             ifr.style.display = 'none';             document.body.appendchild(ifr);              var f = document.createelement('form');             f.setattribute('method',"post");             f.setattribute('action',"savefileas.php");             f.setattribute('target',"target");              addfield(f, "filename", "myfilename");             addfield(f, "text", text);              f.submit();              ifr.onload = cleanup;              function cleanup(){                 document.body.removechild(ifr);                 ifr = null;             }              function addfield(form, name, value) {                 var = document.createelement("input");                 i.setattribute('name',name);                 i.setattribute('type','text');                 i.value = value;                 form.appendchild(i);             }         }  

ggilmann


Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -