[英]Laravel download varbinary file from SQL Server
I am to storing PDF files into varbinary(max) field, using Laravel 5 and SQl server 2016. The probelm is am trying to download the file from the SQL Server, but it continues to return corrupt files. I am to storing PDF files into varbinary(max) field, using Laravel 5 and SQl server 2016. The probelm is am trying to download the file from the SQL Server, but it continues to return corrupt files. When I upload, I use:
当我上传时,我使用:
'file' => DB::raw("CONVERT(VARBINARY(MAX), '" . base64_encode(file_get_contents($upload_file)) . "')"),
When I download, I'm doing this:当我下载时,我正在这样做:
$document = DB::table('files')
->selectRaw('CAST(file AS VARBINARY(MAX)) AS file')->first();
$pdf = base64_decode($document->file);
return response($pdf)
->header('Cache-Control', 'no-cache private')
->header('Content-Description', 'File Transfer')
->header('Content-type','application/pdf;base64')
->header('Content-length', strlen($pdf))
->header('Content-Disposition', 'attachment; filename=Teste')
->header('Content-Transfer-Encoding', 'binary');
The original files has 97kb, and when i download has 187kb.原始文件有97kb,当我下载时有187kb。 Any Help please!
请提供任何帮助!
Resolvede the problem: First i converted to Hexadecimal the file uploaded:解决问题:首先我将上传的文件转换为十六进制:
$datastring = file_get_contents($request->file('upload_file'));
$unpack = unpack("H*hex", $datastring);
$unpack = '0x'.$unpack['hex'];
//store DB
'file' => DB::raw("CONVERT(VARBINARY(MAX), $unpack)"),
Then when download, i had convert back to bin:然后在下载时,我已经转换回 bin:
$file_contents = hex2bin($document->file);
return response($file_contents)
->header('Cache-Control', 'no-cache private')
->header('Content-Description', 'File Transfer')
->header('Content-type','application/pdf;base64')
->header('Content-length', strlen($file_contents))
->header('Content-Disposition', 'attachment; filename=aaaa')
->header('Content-Transfer-Encoding', 'binary');
Thanks.谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.