【发布时间】:2017-04-12 11:00:13
【问题描述】:
我有一个 Symfony 控制器来上传 .docx 文件和另一个来下载它。
我的 symfony 控制器下载文件如下所示:
public function getDocumentationAction(Request $request, $uriFile) {
$filename = $uriFile;
$path = $this->getParameter('documents_directory').'/'.$filename;
$file = file_get_contents($path);
$response = new Response($file);
$response->headers->set('Content-Type', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
$response->headers->set('Content-Description', 'File Transfer');
$response->headers->set('Content-Disposition', 'attachment; filename="'.$filename.'"');
return $response;
}
问题是下载文件时,文件格式不正确。例如,如果我上传一个包含test 字的docx,当我下载这个文件时,我会得到一个奇怪的.docx,开头为:
PK!›¸ï7f [Content_Types].xml ¢(†¥TÀn¬0ºWÍ?DæVâ°á™™˙8∂H•
Ï XıKˆÚ˙˚nDUA*Â)YÔÃÏσɗ⁄öl 1iÔJ÷/z,'Ω“nV≤è…K~œ≤Ѭ)aºÉím ±—˙j0ŸHuªT≤9bx‡<…9Xë ¿Q•Ú— §◊8„A»O1~€Î›qÈÇ√k6<A%≥Á5}fi*âÀ∑kÆíâåñI)_:ıÉ%f1'ŸúIs“
如果我将这个奇怪的 docx 文件转换为 PDF,例如使用:https://online2pdf.com/en/convert-docx-to-pdf,我会得到一个带有 test 字样的 PDF,所以信息在其中,但显示不正确。
我的 Symfony 上传控制器似乎运行良好,因为我可以从服务器访问文件并正确查看内容。
【问题讨论】: