我有一段客户端代码,用于从Google Drive中导出.docx文件并将数据发送到我的服务器。它非常简单直接,它只是导出文件,将其放入Blob中,并将Blob发送到POST端点。为什么我无法从POST请求中提取zip文件?
gapi.client.drive.files.export({
fileId: file_id,
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
}).then(function (response) {
// the zip file data is now in response.body
var blob = new Blob([response.body], {type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"});
// send the blob to the server to extract
var request = new XMLHttpRequest();
request.open('POST', 'return-xml.php', true);
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.onload = function() {
// the extracted data is in the request.responseText
// do something with it
};
request.send(blob);
});
这里是我的服务器端代码到这个文件保存到我的服务器,所以我可以做的事情吧:
<?php
file_put_contents('tmp/document.docx', fopen('php://input', 'r'));
当我运行此,我的服务器上创建的文件。不过,我相信它已损坏,因为当我尝试将它解压缩(你可以用.DOCX做到),出现这种情况:
$ mv tmp/document.docx tmp/document.zip
$ unzip tmp/document.zip
Archive: document.zip
error [document.zip]: missing 192760059 bytes in zipfile
(attempting to process anyway)
error [document.zip]: start of central directory not found;
zipfile corrupt.
(please check that you have transferred or created the zipfile in the
appropriate BINARY mode and that you have compiled UnZip properly)
为什么没有认识到它作为一个适当的.zip文件?
未来读者注意:我仍然不知道如何做到这一点。我想我只是努力将一个拉链文件形状的钉子插入一个存取令牌形状的孔中。所以,我重组了应用程序,在后端进行gapi导出调用,并在那里处理提取的数据。 –