【问题标题】:Upload an input image taken from webapp into google drive with google script使用谷歌脚本将从 webapp 获取的输入图像上传到谷歌驱动器
【发布时间】:2022-02-08 08:33:47
【问题描述】:

I found something similar here,但它似乎比我需要的要复杂得多。

我正在将图像上传到 web 应用程序,我想将图像保存到谷歌驱动器中。

我在以可读格式为DriveApp.getFolderById("xxxxxxxxxxxxx").createFile(file); 传递文件时遇到问题,并且一直出错。我不知道如何以正确的格式传递文件。

HTML:

<input id = "output2" type="file" accept="image/*">
<script>
document.getElementById('output2').addEventListener('change', (e) => triggerF(e.target.files));

</script>


function triggerF(file) {

console.log("event triggered");
console.log(file.name);
google.script.run.withSuccessHandler(yourCallBack1).upload(file)}


function yourCallBack1() {
  console.log("callback called");
  }

GS:

function upload(e) {

const file = new Blob(e);
const filecreat = DriveApp.getFolderById("xxxxxxxxxxxxx").createFile(file);
console.log("created file");

}

更新:Tanaike 的解决方案 #2 对我有用。请在下方查看。

【问题讨论】:

  • 这里是一个收集收据信息包括上传收据图片的例子:stackoverflow.com/a/57020995/7215091
  • 谢谢,我实际上需要它来达到同样的目的。如果没有其他选择,我将使用该示例,但我想了解如何以某种方式传递文件以使我的代码正常工作。
  • 首先,我深表歉意,我的脚本对您的情况没有用处。根据您的问题,我提出了 2 个修改后的脚本作为答案。你能确认一下吗?如果这些没有用,我很抱歉。

标签: javascript google-apps-script


【解决方案1】:

我相信你的目标如下。

  • 来自I am uploading an image to a webapp and I would like to save the image into google drive.,您想使用 Web 应用上传图像文件。

这样的话,下面的修改怎么样?

修改脚本一:

幸运的是,HTML 表单解析的 bug 已被移除。 Ref这样,你就可以使用如下修改后的脚本了。

HTML & Javascript 方面:

<form><input id = "output2" name="file" type="file" accept="image/*"></form>
<script>
document.getElementById('output2').addEventListener('change', (e) => triggerF(e.target.parentNode));

function triggerF(file) {
  console.log("event triggered");
  google.script.run.withSuccessHandler(yourCallBack1).upload(file);
}

function yourCallBack1() {
  console.log("callback called");
}
</script>

Google Apps 脚本方面:

function upload(e) {
  const filecreat = DriveApp.getFolderById("xxxxxxxxxxxxx").createFile(e.file);
  console.log("created file");
}

修改脚本2:

作为另一种方法,您还可以使用以下脚本。

HTML & Javascript 方面:

<input id = "output2" type="file" accept="image/*">
<script>
document.getElementById('output2').addEventListener('change', (e) => triggerF(e.target.files[0]));
function triggerF(file) {
  console.log("event triggered");
  const fr = new FileReader();
  fr.readAsArrayBuffer(file);
  fr.onload = (f) => {
    google.script.run.withSuccessHandler(yourCallBack1).upload([[...new Int8Array(f.target.result)], file.type, file.name]);
  };
}

function yourCallBack1() {
  console.log("callback called");
}
</script>

Google Apps 脚本方面:

function upload(e) {
  const filecreat = DriveApp.getFolderById("xxxxxxxxxxxxx").createFile(Utilities.newBlob(...e));
  console.log("created file");
}

注意:

  • 在您的脚本中,似乎使用了 Web 应用程序。 在这种情况下,当您修改 Google Apps 脚本时,请将部署修改为新版本。这样,修改后的脚本就会反映在 Web Apps 中。请注意这一点。
  • 您可以在“Redeploying Web Apps without Changing URL of Web Apps for new IDE”的报告中查看详细信息。

【讨论】:

  • 谢谢!!我将在今天晚些时候尝试一下,让你知道它是否有效
  • @Filippo 感谢您的回复。如果这没有用,我深表歉意。
  • 我现在正在检查解决方案 1,但我得到“InvalidArgumentError:由于属性中的非法值而失败:0”我认为发送到服务器端的文件格式仍然存在问题.. 我会检查解决方案 2
  • 很好,解决方案 #2 有效!我会首选解决方案#1以保持简单,但仍然存在一些问题。你认为你能纠正#1吗?如果没有,您可以将其删除,我将接受解决方案 #2 的答案。这将使用户将来更容易找到正确的答案
  • @Filippo 感谢您的回复。我不得不为我糟糕的英语水平道歉。不幸的是,我无法从I would have preferred solution #1 to keep things simple, but still some issues there. Do you think you can correct #1? 理解您的问题。可以问一下still some issues there的详细信息吗?使用 Web Apps 时,可以使用模式 1。但是当使用对话框和侧边栏时,不能使用模式1。如果您能原谅我糟糕的英语水平,我将不胜感激。
猜你喜欢
  • 2021-12-16
  • 2020-03-13
  • 1970-01-01
  • 2021-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-23
  • 1970-01-01
相关资源
最近更新 更多