我有一个按钮,可以在客户端下载docx文件。该文件是使用PHPWord生成的。在Chrome中,它可以正常工作,但不适用于Firefox:
JS:
var req = new XMLHttpRequest();
req.open("POST", "/vendor/gendocx.php", true);
req.responseType = "blob";
req.onload = function (event) {
var blob = req.response;
console.log(blob.size);
console.log(blob);
var link=document.createElement('a');
link.href=window.URL.createObjectURL(blob);
link.download="TEST_this out.docx";
link.click();
};
req.send(content);
PHP:
$filename = 'TEST_this out';
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpword, 'Word2007');
header("Content-Description: File Transfer");
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Expires: 0');
$objWriter->save($filename);
$objWriter->save("php://output");
exit;
我已在此网站上尝试过其他解决方案,但没有成功。希望你能帮我解决这个问题。
没有错误消息。这是blob
答案 0 :(得分:0)
我明白了。
显然在firefox上我必须将document.body.appendChild(a)
添加到dom。
req.onload = function (event) {
var blob = req.response;
console.log(blob.size);
console.log(blob);
var link=document.createElement('a');
document.body.appendChild(link);
link.href=window.URL.createObjectURL(blob);
console.log(link.href);
link.download="TEST_this_out.docx";
link.click();
setTimeout(function(){
document.body.removeChild(link);
}, 500)
};
非常感谢你的帮助!