亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

調用Google應用程序腳本上的圖像文件上傳到GoogleDrive

調用Google應用程序腳本上的圖像文件上傳到GoogleDrive

慕田峪7331174 2023-09-04 15:44:23
我正在嘗試設置一種將圖像文件上傳到谷歌驅動器的方法。它將使用 timeid 創建一個文件夾,并將圖像放置在它創建的文件夾中。我在調用圖像文件時遇到問題。這就是我嘗試的方法,創建了文件夾但沒有圖像。請忽略 timeid 變量中任何缺失的 var。這工作正常。給出的錯誤:ReferenceError:imgInp 未定義預先感謝您的幫助!Code.gs  var day = d.getDate();  var month = d.getUTCMonth();  var hour = d.getHours();  var minutes = d.getMinutes();  var realmonth = month+1;  var timeid = String(year)+"-"+String(realmonth)+"-"+String(day)+"-"+String(hour)+"-"+String(minutes);  var foldername=timeid;  var parentFolder=DriveApp.getFolderById("##############");function upload(){  var newFolder=parentFolder.createFolder(timeid);  var folderidlookup = newFolder.getId();  var destination = DriveApp.getFolderById(folderidlookup);  var imgf = imgInp;  var contentType = 'image/jpeg';  var imgf = imgf.getAs(contentType);  destination.createFile(imgf)}網頁       <form>        <div class="file-field input-field">         <div class="waves-effect waves-light btn" id="wholebtn"><i class="material-icons right">cloud</i>Browse         <input type="file" name="imgInp" id="imgInp" onchange="loadFile(event)">        </div>         <div class="file-path-wrapper">         <input type="text" class="file-path">        </div>        </div>       </form>    <button class="btn waves-effect waves-light" type="submit" name="action" id ="button">Submit      <i class="material-icons right">send</i>    </button>JS<script>  document.getElementById("button").addEventListener("click",upload);   function upload(){     google.script.run.upload();  }</script>
查看完整描述

2 回答

?
狐的傳說

TA貢獻1804條經驗 獲得超3個贊

您收到的錯誤是因為您嘗試使用一個imgInp在代碼的任何部分都沒有定義的變量。您可以從輸入中獲取 blob 文件,將其轉換為二進制數組字符串,將其傳遞到服務器端,最后使用它來創建您的 blob 和給定的 Drive 文件。

使用HTML 服務指南中有關如何使用表單以及成功和失敗處理程序的示例,我整理了以下代碼,該代碼可以成功上傳給定圖像:

索引.html

<!DOCTYPE html>

<html>

<head>

? ? <base target="_top">

</head>

<body>

? ? <form>

? ? ? ? <div class="file-field input-field">

? ? ? ? ? ? <div class="waves-effect waves-light btn" id="wholebtn"><i class="material-icons right">cloud</i>Browse

? ? ? ? ? ? ? ? <input type="file" name="imgInp" id="imgInp">

? ? ? ? ? ? </div>

? ? ? ? ? ? <div class="file-path-wrapper">

? ? ? ? ? ? ? ? <input type="text" class="file-path">

? ? ? ? ? ? </div>

? ? ? ? </div>

? ? ? ? <button class="btn waves-effect waves-light" name="action" id="button">Submit

? ? ? ? ? ? <i class="material-icons right">send</i>

? ? ? ? </button>

? ? </form>

? ? <script>

? ? ? ? // Prevent forms from submitting.

? ? ? ? function preventFormSubmit() {

? ? ? ? ? ? var forms = document.querySelectorAll('form');

? ? ? ? ? ? for (var i = 0; i < forms.length; i++) {

? ? ? ? ? ? ? ? forms[i].addEventListener('submit', function(event) {

? ? ? ? ? ? ? ? ? ? event.preventDefault();

? ? ? ? ? ? ? ? });

? ? ? ? ? ? }

? ? ? ? }


? ? ? ? // Add event listeners?

? ? ? ? window.addEventListener('load', preventFormSubmit);

? ? ? ? document.getElementById("button").addEventListener("click", upload);


? ? ? ? // Handler function

? ? ? ? function logger(e) {

? ? ? ? ? ? console.log(e)

? ? ? ? }


? ? ? ? async function upload() {

? ? ? ? ? ? // Get all the file data

? ? ? ? ? ? let file = document.querySelector('input[type=file]').files[0];


? ? ? ? ? ? // Get binary content, we have to wait because it returns a promise?

? ? ? ? ? ? let fileBuffer = await file.arrayBuffer();

? ? ? ? ? ? // Get the file content as binary and then convert it to string?

? ? ? ? ? ? const data = (new Uint8Array(fileBuffer)).toString();

? ? ? ? ? ? // Pass the binary array string to uploadG funciton on code.gs

? ? ? ? ? ? google.script.run.withFailureHandler(logger).withSuccessHandler(logger).uploadG(data);

? ? ? ? }

? ? </script>

</body>

</html>

Code.gs


function doGet() {

? return HtmlService.createHtmlOutputFromFile('Index');

}


function uploadG(imgInp){

? var parentFolder=DriveApp.getFolderById("[FOLER-ID]");

? var newFolder = parentFolder.createFolder('test webApp');

? var folderidlookup = newFolder.getId();


? var destination = DriveApp.getFolderById(folderidlookup);

? var contentType = 'image/jpeg';

? // Convert the binary array string to array and use it to create the Blob

? var blob = Utilities.newBlob(JSON.parse("[" + imgInp + "]"), contentType);

? blob = blob.getAs(contentType);

? destination.createFile(blob)


? return 'Filed uploaded!';

}


查看完整回答
反對 回復 2023-09-04
?
牧羊人nacy

TA貢獻1862條經驗 獲得超7個贊

文件上傳對話框


從腳本編輯器運行upLoadMyDialog()以開始使用。選擇文件并單擊上傳。


function fileUpload(obj) {

  var d=new Date();

  var ts=Utilities.formatDate(d, Session.getScriptTimeZone(), "yyyy-MM-dd-HH-mm");

  var folder=DriveApp.getFolderById("****** Enter FolderId *******");

  var file=folder.createFile(obj.file1).setName(ts);

}


function uploadMyDialog() {

  var ss=SpreadsheetApp.getActive();

  var html='<form><input type="file" name="file1"/><br /><input type="button" value="Upload" onClick="google.script.run.fileUpload(this.parentNode);" /></form>';  

  SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(html),"Upload File");

}

使用事件監聽器:


function uploadMyDialog() {

  var ss=SpreadsheetApp.getActive();

  var html='<form id="f1"><input type="file" name="file1"/><br /><input type="button" value="Upload" id="btn1" /></form>'; 

  html+='<script>window.onload=function(){document.getElementById("btn1").addEventListener("click",function(){google.script.run.fileUpload(document.getElementById("f1"))})}</script>';

  SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(html),"Upload File");

}


查看完整回答
反對 回復 2023-09-04
  • 2 回答
  • 0 關注
  • 177 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號