使用Google Apps脚本将文件上传到Google云端硬盘

时间:2020-09-07 19:01:48

标签: html forms google-apps-script

尝试创建Google Apps脚本,以提示我选择要上传到Google云端硬盘的文件,如here

但是我收到一个错误

未捕获的ScriptError:TypeError:无法读取的属性'getAs' 未定义

我已经看过几次YouTube视频,但是我看不到我做错了什么。有什么想法吗?

function doGet(){
  return HtmlService.createHtmlOutputFromFile("form.html")
}

function upload(e){
  // logic to upload the file
  var destination_id = 'enter your folder id here' //folder id
  var img = e.imageFile
  var contentType = 'img/png'
  var destination = DriveApp.getFolderById(destination_id)
  var img = img.getAs(contentType)
  destination.createFile(img)
  
}
<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <form>
    <input type="file" name="imageFile"/>
    <input type="button" value="Upload" onclick="google.script.run.upload(this.parentNode)">
  
    </form>
    
  </body>
</html>

1 个答案:

答案 0 :(得分:-1)

代码有错误

替换

var contentType = 'img/png'

作者

var contentType = 'image/png'

应用上述修复程序后,代码对我来说效果很好。

注意:尽管使用默认的运行时(Chrome V8)代码不会引发任何错误,但文件未正确上传,但使用旧的运行程序(Mozilla Rhino)可以正常工作。

关于遇到的特定错误,请说明您没有将问题中显示的代码发布为Web应用程序的新版本。尝试使用链接测试最新代码。

相关

相关问题