1 (edited by mantas.s 2012-05-10 3:09:11 PM)

Topic: stream data downloaded as file

hello, i'm using PHP 5.3.6 && xajax 0.5 and

i'm looking for a sollution for the following idea:

1) some person clicks an image on the web page,
2) and the browser "save as" dialog appears,
3) user saves file to local storage

mean while on the server side the following approach is required:
1) xajax registered php function gets launched
2) php varible

 $x = "Text inside file"; 

get's sent as if it was a text file to be saved (and not opened in the browser) window.

Any sugguestinos?

Edit:
p.s. http://www.apptools.com/phptools/force-download.php describes how to send file as an attachment
php part:

header('Content-Type: text/plain');
header('Content-Length: X);
header('Content-Disposition: attachment;filename="test.jpg"');

should do the job well,
however how do i edit Content-Disposition, content-length and content-type in xajax response?
xajaxResponse has function

contentType

for receiving type, what about setting / controling?

Re: stream data downloaded as file

Unfortunately what  your are trying to achieve is not possible with ajax requests. An Ajax request always requires a valid response in either XML or JSON. Binary mimetypes aren't possible. The only thing you can do is to redirect the user to a download script that forces the client to download a file. For instance: $objrResponse->redirect('/download.php?file=123');

Re: stream data downloaded as file

Thanks for information.

Re: stream data downloaded as file

working with php + apache sniplets of code, in case someone might need it:

//register xajax function
$x->xajax->register(XAJAX_FUNCTION, array('downRep', $this, 'downRepClick'));

#Download report click. Needs error handling / output to UI
   function downRepClick($formVals){
      $r = new xajaxResponse();
      $r->redirect(URL);//url is build using certain code rules, depending on the needs, in this example it's just a constant
      return $r;
   }

#sends string stream as file
   function send($devInfo){
     $this->streamHeader("text/plain");
     $this->dump_chunk("test");
     $this->dump_chunk("end test");
     print "0\r\n"; #chunck ends with zero length and final \r\n
   }

function streamHeader($contentType,){
      header("Content-Type: $contentType"); #1
      #header("Keep-Alive: timeout=5, max=99");
      header("Connection: Keep-Alive");
      header('Content-Disposition: attachment; filename="test.txt"');   
      #header('Content-Transfer-Encoding: 8bit');
      header('Transfer-Encoding: chunked');#instead of print header('Content-Length: 16'); etc
   }

   
   #usefull function found somewhere on the net
   function dump_chunk($chunk){
                echo sprintf("%x\r\n", strlen($chunk));#print dechex(strlen($chunk))."\r\n";
                echo $chunk;
                echo "\r\n";
   }

#can't manage to configure IIS to work with this code yet... download box starts, however no stream is downloaded. If i lounch fiddler to investigate a situation - file gets downloaded, as if fiddler did some kind of corrections...