1 回答

TA貢獻1780條經驗 獲得超1個贊
這個改裝怎么樣?
修改點:
似乎不能直接
multipart/form-data
使用ajax 請求FormData()
。所以在這種情況下,需要創建結構multipart/form-data
并將其作為數據發送。在您的腳本中,僅上傳文件內容而沒有文件元數據。這樣,上傳的文件就沒有文件名了。在這種情況下,需要上傳文件內容和文件元數據
multipart/form-data
。在您的腳本中,有 2 個屬性
data
。
當以上幾點反映到您的腳本中時,它會變成如下。
修改腳本:
Upload.prototype.doUpload = function () {
const file = this.file; // It supposes that "this.file" is the blob.
const fr = new FileReader();
fr.readAsDataURL(file);
fr.onload = function() {
const boundary = "xxxxxxxxxx";
let data = "--" + boundary + "\n";
data += "Content-Type: application/json; charset=UTF-8\n\n";
data += JSON.stringify({name: "test_file"}) + "\n";
data += "--" + boundary + "\n";
data += "Content-Transfer-Encoding: base64\n\n";
data += fr.result.split(",")[1] + "\n";
data += "--" + boundary + "--";
$.ajax({
type: "POST",
beforeSend: function(request) {
request.setRequestHeader("Authorization", "Bearer" + " " + localStorage.getItem("accessToken"));
request.setRequestHeader("Content-Type", "multipart/related; boundary=" + boundary);
},
url: "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart",
success: function (data) {
console.log(data);
},
error: function (error) {
console.log(error);
},
async: true,
data: data,
cache: false,
processData: false,
timeout: 60000
});
}
}
筆記:
在這個修改后的腳本中,
它假定
this.file
在您的腳本中是 blob。您的訪問令牌可用于將文件上傳到 Google 云端硬盤。
使用
uploadType=multipart
時,最大文件大小為 5 MB。請注意這一點。
添加回答
舉報