我有一段客戶端代碼,用於從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導出調用,並在那裏處理提取的數據。 –