第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

控制 Google 云端硬盤中的文件上傳位置(應用腳本而非表單)

控制 Google 云端硬盤中的文件上傳位置(應用腳本而非表單)

梵蒂岡之花 2023-05-11 16:50:33
我正在實施 Kanshi Tanaike 的Web 應用程序代碼的可恢復上傳并且它有效,但我不完全理解 AJAX 并且正在嘗試添加功能?,F(xiàn)在代碼將新文件放在用戶的 Drive 根文件夾中。我想定義一個特定的文件夾并直接上傳到那里,或者自動將文件從根目錄移動到正確的文件夾(我還需要收集下載鏈接)。我在響應標頭中看到上傳函數(shù)引用位置,但我正在努力弄清楚如何定義它,并且由于 doUpload() 函數(shù)似乎沒有將上傳視為 File 對象,我不知道如何定義上傳后引用它以獲取 URL 或移動它。任何反饋將不勝感激。 $('#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() {    $("#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");        xhr.setRequestHeader('Authorization', "Bearer " + at);        xhr.setRequestHeader('Content-Type', "application/json");        xhr.send(JSON.stringify({            mimeType: fileType,            name: fileName,        }));        xhr.onload = function() {            doUpload({                location: xhr.getResponseHeader("location"),                chunks: chunks,            });        };        xhr.onerror = function() {            console.log(xhr.response);        };    }).getAt();
查看完整描述

1 回答

?
aluckdog

TA貢獻1847條經(jīng)驗 獲得超7個贊

我相信你的目標和你目前的情況如下。

  • 您要將文件上傳到特定文件夾。

  • 您想要檢索webContentLink上傳的文件。

  • 您想使用Google Apps 腳本使用 Web 應用程序的可恢復上傳來實現(xiàn)上述目標

  • 您已經(jīng)確認存儲庫中的默認腳本有效。

修改點:

  • 在這種情況下,需要勾選斷點續(xù)傳和Drive API中?“Files: create”的方法。

    • 為了將文件上傳到特定的文件夾,請在初始請求的請求正文中添加文件夾ID。

    • 為了返回 的值webContentLink,請使用fieldsvalue 到初始請求。

將以上幾點反映到原稿中,就變成了下面這樣。

修改腳本:

在這種情況下,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顯示為結果。



查看完整回答
反對 回復 2023-05-11
  • 1 回答
  • 0 關注
  • 155 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號