1 回答

TA貢獻1847條經(jīng)驗 獲得超7個贊
我相信你的目標和你目前的情況如下。
您要將文件上傳到特定文件夾。
您想要檢索
webContentLink
上傳的文件。您想使用Google Apps 腳本使用 Web 應用程序的可恢復上傳來實現(xiàn)上述目標
您已經(jīng)確認存儲庫中的默認腳本有效。
修改點:
在這種情況下,需要勾選斷點續(xù)傳和Drive API中?“Files: create”的方法。
為了將文件上傳到特定的文件夾,請在初始請求的請求正文中添加文件夾ID。
為了返回 的值
webContentLink
,請使用fields
value 到初始請求。
將以上幾點反映到原稿中,就變成了下面這樣。
修改腳本:
在這種情況下,HTML被修改。
<!DOCTYPE html>
<html>
<head>
? ? <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.js"></script>
? ? <title>Resumable upload for Web Apps</title>
</head>
<body>
? ? <form>
? ? ? ? <input name="file" id="uploadfile" type="file">
? ? </form>
? ? <div id="progress"></div>
<script>
? ? const chunkSize = 5242880;
? ? $('#uploadfile').on("change", function() {
? ? ? ? var file = this.files[0];
? ? ? ? if (file.name != "") {
? ? ? ? ? ? var fr = new FileReader();
? ? ? ? ? ? fr.fileName = file.name;
? ? ? ? ? ? fr.fileSize = file.size;
? ? ? ? ? ? fr.fileType = file.type;
? ? ? ? ? ? fr.onload = init;
? ? ? ? ? ? fr.readAsArrayBuffer(file);
? ? ? ? }
? ? });
? ? function init() {
? ? ? ? var folderId = "###";? // Added: Please set the folder ID.
? ??
? ? ? ? $("#progress").text("Initializing.");
? ? ? ? var fileName = this.fileName;
? ? ? ? var fileSize = this.fileSize;
? ? ? ? var fileType = this.fileType;
? ? ? ? console.log({fileName: fileName, fileSize: fileSize, fileType: fileType});
? ? ? ? var buf = this.result;
? ? ? ? var chunkpot = getChunkpot(chunkSize, fileSize);
? ? ? ? var uint8Array = new Uint8Array(buf);
? ? ? ? var chunks = chunkpot.chunks.map(function(e) {
? ? ? ? ? ? return {
? ? ? ? ? ? ? ? data: uint8Array.slice(e.startByte, e.endByte + 1),
? ? ? ? ? ? ? ? length: e.numByte,
? ? ? ? ? ? ? ? range: "bytes " + e.startByte + "-" + e.endByte + "/" + chunkpot.total,
? ? ? ? ? ? };
? ? ? ? });
? ? ? ? google.script.run.withSuccessHandler(function(at) {
? ? ? ? ? ? var xhr = new XMLHttpRequest();
? ? ? ? ? ? xhr.open("POST", "https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable&fields=*");
? ? ? ? ? ? xhr.setRequestHeader('Authorization', "Bearer " + at);
? ? ? ? ? ? xhr.setRequestHeader('Content-Type', "application/json");
? ? ? ? ? ? xhr.send(JSON.stringify({
? ? ? ? ? ? ? ? mimeType: fileType,
? ? ? ? ? ? ? ? name: fileName,
? ? ? ? ? ? ? ? parents: [folderId]? // Added
? ? ? ? ? ? }));
? ? ? ? ? ? xhr.onload = function() {
? ? ? ? ? ? ? ? doUpload({
? ? ? ? ? ? ? ? ? ? location: xhr.getResponseHeader("location"),
? ? ? ? ? ? ? ? ? ? chunks: chunks,
? ? ? ? ? ? ? ? });
? ? ? ? ? ? };
? ? ? ? ? ? xhr.onerror = function() {
? ? ? ? ? ? ? ? console.log(xhr.response);
? ? ? ? ? ? };
? ? ? ? }).getAt();
? ? }
? ? function doUpload(e) {
? ? ? ? var chunks = e.chunks;
? ? ? ? var location = e.location;
? ? ? ? var cnt = 0;
? ? ? ? var end = chunks.length;
? ? ? ? var temp = function callback(cnt) {
? ? ? ? ? ? var e = chunks[cnt];
? ? ? ? ? ? var xhr = new XMLHttpRequest();
? ? ? ? ? ? xhr.open("PUT", location, true);
? ? ? ? ? ? xhr.setRequestHeader('Content-Range', e.range);
? ? ? ? ? ? xhr.send(e.data);
? ? ? ? ? ? xhr.onloadend = function() {
? ? ? ? ? ? ? ? var status = xhr.status;
? ? ? ? ? ? ? ? cnt += 1;
? ? ? ? ? ? ? ? console.log("Uploading: " + status + " (" + cnt + " / " + end + ")");
? ? ? ? ? ? ? ? $("#progress").text("Uploading: " + Math.floor(100 * cnt / end) + "%");
? ? ? ? ? ? ? ? if (status == 308) {
? ? ? ? ? ? ? ? ? ? callback(cnt);
? ? ? ? ? ? ? ? } else if (status == 200) {
? ? ? ? ? ? ? ? ? ? var metadata = JSON.parse(xhr.response);? // Added
? ? ? ? ? ? ? ? ? ? $("#progress").text("Done. Link: " + metadata.webContentLink);? // Modified
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? $("#progress").text("Error: " + xhr.response);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? };
? ? ? ? }(cnt);
? ? }
? ? function getChunkpot(chunkSize, fileSize) {
? ? ? ? var chunkPot = {};
? ? ? ? chunkPot.total = fileSize;
? ? ? ? chunkPot.chunks = [];
? ? ? ? if (fileSize > chunkSize) {
? ? ? ? ? ? var numE = chunkSize;
? ? ? ? ? ? var endS = function(f, n) {
? ? ? ? ? ? ? ? var c = f % n;
? ? ? ? ? ? ? ? if (c == 0) {
? ? ? ? ? ? ? ? ? ? return 0;
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? return c;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }(fileSize, numE);
? ? ? ? ? ? var repeat = Math.floor(fileSize / numE);
? ? ? ? ? ? for (var i = 0; i <= repeat; i++) {
? ? ? ? ? ? ? ? var startAddress = i * numE;
? ? ? ? ? ? ? ? var c = {};
? ? ? ? ? ? ? ? c.startByte = startAddress;
? ? ? ? ? ? ? ? if (i < repeat) {
? ? ? ? ? ? ? ? ? ? c.endByte = startAddress + numE - 1;
? ? ? ? ? ? ? ? ? ? c.numByte = numE;
? ? ? ? ? ? ? ? ? ? chunkPot.chunks.push(c);
? ? ? ? ? ? ? ? } else if (i == repeat && endS > 0) {
? ? ? ? ? ? ? ? ? ? c.endByte = startAddress + endS - 1;
? ? ? ? ? ? ? ? ? ? c.numByte = endS;
? ? ? ? ? ? ? ? ? ? chunkPot.chunks.push(c);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? } else {
? ? ? ? ? ? var chunk = {
? ? ? ? ? ? ? ? startByte: 0,
? ? ? ? ? ? ? ? endByte: fileSize - 1,
? ? ? ? ? ? ? ? numByte: fileSize,
? ? ? ? ? ? };
? ? ? ? ? ? chunkPot.chunks.push(chunk);
? ? ? ? }
? ? ? ? return chunkPot;
? ? }
</script>
</body>
</html>
運行上述修改后的腳本時,上傳的文件將創(chuàng)建到特定文件夾并webContentLink顯示為結果。
添加回答
舉報