Gestion de 'ModuleRequest->download()' si c'est une requête ajax (header 'X-Requested-With'), on renvoie 'link' qui pointe vers un fichier temporaire php qui, lui, affichera le fichier, puis s'autosupprimera ('unlink(__FILE__)')
This commit is contained in:
parent
15b1aa47f7
commit
2373d2c5bc
|
@ -1,3 +1,3 @@
|
||||||
function APIClass(b){this.target=b}
|
function APIClass(b){this.target=b}
|
||||||
APIClass.prototype={xhr:[],buffer:null,send:function(b,e,g){b.hasOwnProperty("path")||e({ModuleError:4});for(var a=0;a<this.xhr.length;a++)4==this.xhr[a].readyState&&(this.xhr=this.xhr.slice(0,a-1).concat(this.xhr.slice(a,this.xhr.length-1)));this.xhr.push(null);a=this.xhr.length-1;this.xhr[a]=window.XMLHttpRequest?new XMLHttpRequest:new ActiveXObject("Microsoft.XMLHttpRequest");console.log(b);var d=this;this.xhr[a].onreadystatechange=function(){if(4==d.xhr[a].readyState)if(d.buffer=d.xhr[a].responseText,
|
APIClass.prototype={xhr:[],buffer:null,send:function(b,e,g){b.hasOwnProperty("path")||e({ModuleError:4});for(var a=0;a<this.xhr.length;a++)4==this.xhr[a].readyState&&(this.xhr=this.xhr.slice(0,a-1).concat(this.xhr.slice(a,this.xhr.length-1)));this.xhr.push(null);a=this.xhr.length-1;this.xhr[a]=window.XMLHttpRequest?new XMLHttpRequest:new ActiveXObject("Microsoft.XMLHttpRequest");console.log(b);var d=this;this.xhr[a].onreadystatechange=function(){if(4==d.xhr[a].readyState)if(d.buffer=d.xhr[a].responseText,
|
||||||
-1<[0,200].indexOf(d.xhr[a].status))try{e(JSON.parse(d.xhr[a].responseText))}catch(b){e({ModuleError:-1,ErrorDescription:"Erreur au niveau de api.js"}),console.warn(b)}else e({ModuleError:3})};var f=new FormData,c;for(c in b)"path"==c?f.append(c,b[c]):b[c]instanceof File?f.append(c,b[c]):f.append(c,JSON.stringify(b[c]));this.xhr[a].open("POST",this.target,!0);null!=g&&this.xhr[a].setRequestHeader("Authorization","Digest "+g);this.xhr[a].send(f)}};
|
-1<[0,200].indexOf(d.xhr[a].status))try{e(JSON.parse(d.xhr[a].responseText))}catch(b){e({ModuleError:-1,ErrorDescription:"Erreur au niveau de api.js"}),console.warn(b)}else e({ModuleError:3})};var f=new FormData,c;for(c in b)"path"==c?f.append(c,b[c]):b[c]instanceof File?f.append(c,b[c]):f.append(c,JSON.stringify(b[c]));this.xhr[a].open("POST",this.target,!0);null!=g&&this.xhr[a].setRequestHeader("Authorization","Digest "+g);this.xhr[a].setRequestHeader("X-Requested-With","XMLHttpRequest");this.xhr[a].send(f)}};
|
||||||
|
|
|
@ -92,6 +92,10 @@ APIClass.prototype = {
|
||||||
if( pToken != null ) this.xhr[i].setRequestHeader('Authorization', 'Digest '+pToken);
|
if( pToken != null ) this.xhr[i].setRequestHeader('Authorization', 'Digest '+pToken);
|
||||||
|
|
||||||
|
|
||||||
|
// Header pour dire que c'est AJAX
|
||||||
|
this.xhr[i].setRequestHeader('X-Requested-With', 'XMLHttpRequest');
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
this.xhr[i].send( form );
|
this.xhr[i].send( form );
|
||||||
|
|
||||||
|
|
|
@ -192,8 +192,47 @@
|
||||||
if( !isset($returned['headers']) || !is_array($returned['headers']) )
|
if( !isset($returned['headers']) || !is_array($returned['headers']) )
|
||||||
$returned['headers'] = array();
|
$returned['headers'] = array();
|
||||||
|
|
||||||
/* [5] Gestion du download
|
|
||||||
|
$fromAjax = isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
|
||||||
|
|
||||||
|
/* [5] Si la requête vient d'ajax on crée un fichier temporaire et on renvoie son URL
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
|
if( $fromAjax ){
|
||||||
|
|
||||||
|
$tmpfname = '/tmp/download_'.uniqid().'.php';
|
||||||
|
|
||||||
|
/* (1) On crée le fichier temporaire */
|
||||||
|
$tmpfnameroot = __ROOT__.$tmpfname;
|
||||||
|
$tmpfile = fopen($tmpfnameroot, 'w');
|
||||||
|
|
||||||
|
fwrite($tmpfile, '<?php'.PHP_EOL);
|
||||||
|
|
||||||
|
/* (2) Script qui écrira les headers */
|
||||||
|
foreach($returned['headers'] as $header=>$value)
|
||||||
|
fwrite($tmpfile, "header(\"$header: $value\");".PHP_EOL);
|
||||||
|
|
||||||
|
/* (3) Script qui écrira le contenu */
|
||||||
|
$bodyWithoutQuotes = str_replace("'", "\\'", utf8_decode($returned['body']) );
|
||||||
|
fwrite($tmpfile, "echo <<< EOF".PHP_EOL);
|
||||||
|
fwrite($tmpfile, $bodyWithoutQuotes.PHP_EOL);
|
||||||
|
fwrite($tmpfile, "EOF;".PHP_EOL);
|
||||||
|
|
||||||
|
/* (4) Script qui supprimera le fichier */
|
||||||
|
fwrite($tmpfile, 'unlink(__FILE__);'.PHP_EOL);
|
||||||
|
|
||||||
|
fwrite($tmpfile, '?>'.PHP_EOL);
|
||||||
|
|
||||||
|
/* (5) On ferme le fichier */
|
||||||
|
fclose($tmpfile);
|
||||||
|
|
||||||
|
$response = new ModuleResponse(ManagerError::Success);
|
||||||
|
$response->append('link', $tmpfname);
|
||||||
|
|
||||||
|
return $response;
|
||||||
|
|
||||||
|
/* [6] Gestion du download direct si pas AJAX
|
||||||
|
=========================================================*/
|
||||||
|
}else{
|
||||||
/* (1) On définit les headers */
|
/* (1) On définit les headers */
|
||||||
foreach($returned['headers'] as $header=>$value)
|
foreach($returned['headers'] as $header=>$value)
|
||||||
header($header.': '.$value);
|
header($header.': '.$value);
|
||||||
|
@ -203,6 +242,7 @@
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
File diff suppressed because one or more lines are too long
|
@ -1 +1 @@
|
||||||
var exportDeflater=new FormDeflater(document.getElementById("export-form"),["input"],["data-name"]);$("#export-form #export-all").addEventListener("click",function(b){"1"==exportDeflater.deflate().phone&&api.send({path:"download/phone",subjects:[1,137]},function(a){console.log(a)})},!1);
|
var exportDeflater=new FormDeflater(document.getElementById("export-form"),["input"],["data-name"]);$("#export-form #export-all").addEventListener("click",function(b){"1"==exportDeflater.deflate().phone&&api.send({path:"download/phone",subjects:[1,137]},function(a){if(0!=a.ModuleError)return!1;document.location=a.link})},!1);
|
||||||
|
|
|
@ -17,7 +17,13 @@ $('#export-form #export-all').addEventListener('click', function(e){
|
||||||
if( deflated.phone == '1' ){
|
if( deflated.phone == '1' ){
|
||||||
|
|
||||||
api.send({ 'path': 'download/phone', 'subjects': [1, 137] }, function(res){
|
api.send({ 'path': 'download/phone', 'subjects': [1, 137] }, function(res){
|
||||||
console.log(res);
|
|
||||||
|
// Si erreur
|
||||||
|
if( res.ModuleError != 0 )
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// Sinon on lance le téléchargement
|
||||||
|
document.location = res.link;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue