2016-04-06 31 views
1

Bir REST API'sinden bir Byte Array alıyorum. Sonra bu bayt dizisinden yerel bir dosya oluşturuyorum. Ve bu dosyayı ZF2 ile tarayıcıya gönderebilirim. ZF2 denetleyicisi eylem içerisindenZF2 Bayt dizisi ile akış

Kodu:

file_put_contents($fullpath, $rawPdf); 
$headers = new \Zend\Http\Headers(); 
$contentDisposition = ($view == 'inline') ? 'inline': 'attachment'; 
$headers->addHeaders(array(
    'Content-Disposition' => $contentDisposition . '; filename="' . basename($fullpath) . '"', 
    'Content-Type' => 'application/pdf', 
    'Content-Length' => filesize($fullpath), 
    'Expires' => '@0', 
    'Cache-Control' => 'must-revalidate', 
    'Pragma' => 'public' 
)); 
$response = new \Zend\Http\Response\Stream(); 
$response->setStream(fopen($fullpath, 'r')); 
$response->setStreamName(basename($fullpath)); 
$response->setStatusCode(200); 
$response->setHeaders($headers); 
return $response; 

Ama doğrudan yerel bir dosya oluşturmadan tarayıcıya bayt dizisi gönderir.

Akışı ($ response-> setStream()) Byte Array ile nasıl ayarlayabilirim? Byte Array ile bir kaynak oluşturmak, ancak yerel bir dosya oluşturmadan nasıl oluşturulur?

düz eski PHP ile bunu yapabilirim:

$rawPdf = ''; 
array_walk($byteArray, function($value) use (&$rawPdf) { 
    $rawPdf .= chr($value); 
}); 
header('Content-type: application/pdf'); 
header('Content-Disposition: inline; filename="Bericht.pdf"'); 
header('Content-Transfer-Encoding: binary'); 
header('Accept-Ranges: bytes'); 
echo $rawPdf; 
+0

Sana $ Yanıt niteliğinde> setContent ($ rawPdf) yapmak zorunda düşünüyorum istediğini uyacak şekilde adapte edebilir; – peterpeterson

+0

Bir dosyayı "php: // output" ve "force" dosyasına yazabilirsiniz – ceadreak

cevap

1

Bu bir CSV dosyasındaki bazı verileri dışa ve otomatik eğer indirmek için kullanmak kod basitleştirilmiş bir örnektir.

Sen

header('Content-Type: text/csv'); 
header('Content-Disposition: attachment;filename="csv_file.csv'); 
header('Cache-Control: max-age=0'); 

$file = fopen('php://output', 'w'); 

foreach ($this->getData() as $data) 
{ 
    fputcsv($file, $data); 
} 

fclose($file); 
exit(0);