我试图在jquery的$ .ajax请求时发送php服务器生成的zip文件。
这是我的代码:
PHP:
$file = tempnam($a_folder_path, "zip");
$zip = new ZipArchive();
$zip->open($file, ZipArchive::OVERWRITE);
$zip->addFile($path_to_json, 'data.json');
$zip->close();
rename($file, $file . '.zip');
$filename = basename($file . '.zip');
$filepath = $file . '.zip';
while (ob_get_level()) {
ob_end_clean();
}
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: public, must-revalidate, post-check=0, pre-check=0");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$filename."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filepath));
ob_end_flush();
echo file_get_contents($filepath);
//@readfile($filepath);
的javascript:
$.ajax(
{
url: myUrl,
type: 'POST',
data: {
"leData" : "some_data"
},
context: document.body,
cache: false,
success: function(data) {
console.log(data);
console.log(data.length);
var bytes = new Uint8Array(data.length);
for (var i=0; i<data.length; i++) {
bytes[i] = data.charCodeAt(i);
}
blob = new Blob([bytes], {type: "application/zip"})
saveAs(blob, "test.zip");//trying to access data with FileSave.js
zip.load(data);//trying to access data with JSZip.js
jsonData = JSON.parse(zip.file('shouldBeThere.json').asText());
},
error: function() {
alert('error');
}
}
);
会发生什么:
- 服务器创建的zip文件,我问它,并且这个文件没有损坏。其中包含shouldBeThere.json。
- 服务器将数据发送到前端。
- console.log(data);在JavaScript中打印一个字符串与我通过使用文本编辑器打开在服务器上创建的zip文件所得到的几乎相同。
- console.log(data.length);根据chrome的devtools,在javascript中输入的行数小于响应头的内容长度。也许提示数据损坏。
- saveAs使用正确的名称创建一个包含文件的zip文件,但是当我尝试解压缩文件时,7zip显示错误:“尝试在文件开始之前移动文件指针”。 6.JSZip似乎加载数据,但然后zip.file('shouldBeThere.json')为空。
问题是我不知道如果问题来自PHP或从JavaScript。我不知道如果PHP发送损坏的zip或如果JavaScript不正确读取它。
我已经尝试了所有在互联网上找到的php头组合和方法。我也尝试在javascript中做不同的事情:使用ArrayBuffer而不是Uint8Array,在Blob()中使用{type:“application/octet-stream”}将字节而不是数据传递给zip.load()。
AJAX是不适合这个。你可以使用'窗口。打开(url);'URL是链接到你的php文件的地方。由于它下载一个文件,窗口将会弹出一秒钟。 – Slim
类似的问题已经在这里找到答案: http://stackoverflow.com/questions/23676748/download-zip-file-with-jquery-from-ajax-post-request – Erick
首先,检查问题来自PHP。 转到您可以下载zip文件的URL(直接下载)并检查是否可以解压文件。 如果是的话,问题来自JS。 查看你的代码后,你不会设置ajax“响应类型”,默认是“text/html”...对于一个大文件,这不是文本而是二进制,你可以使用“Blob”或“arraybuffer”但我不确定,你必须创建一个JSFiddle – Jordan