简体   繁体   English

如何使用Google应用脚本一次将多个文件上传到Google云端硬盘?

[英]How to upload several files at once to Google Drive using Google app script?

I am trying to Upload several files at once to Google Drive using Google app script. 我正在尝试使用Google应用程序脚本一次将多个文件上传到Google云端硬盘。 I have created an upload.html page that contain - 我创建了一个upload.html页面,其中包含-

    <input type="file" name="myFile" id="file-select" multiple/>

But I need to change more stuff in the JavaScript part either, and i got no idea what to change and i haven't find a documentation about the issue. 但是我也需要在JavaScript部分中更改更多内容,而且我不知道要更改什么,也没有找到有关此问题的文档。

I need to deal with each file separately in the 'Code.gs' file. 我需要在“ Code.gs”文件中分别处理每个文件。

For example: 例如:

In the .gs file I got 在.gs文件中

    var file = form.myFile; 

But unfortunately this refers to the first file I uploaded and not to all of them. 不幸的是,这是指我上传的第一个文件,而不是全部。 How do i refer all of them? 我如何推荐所有这些人?

The question is still relevant. 这个问题仍然有意义。 Thanks in advance to anyone that can help! 在此先感谢任何可以提供帮助的人!

I'm wondering if Caja is somehow not allowing the multiple file ability for the Input element. 我想知道Caja是否以某种方式不允许Input元素具有多文件功能。 Caja is a sanitation program used on the HTML. Caja是HTML上使用的卫生程序。

Google Caja Developers Page Google Caja开发人员页面

If you look at this documentation: 如果您查看此文档:

Mozilla Documentation - File List Mozilla文档-文件列表

You should be able to access all the file name in the INPUT element, and iterate the contents. 您应该能够访问INPUT元素中的所有文件名,并对其内容进行迭代。 Loop through all the files in the INPUT element using client side code instead of Google .gs server side code. 使用客户端代码而不是Google .gs服务器端代码循环遍历INPUT元素中的所有文件。

// Get all the files out of the INPUT element
var fileInput = document.getElementById("file-select");
console.log('fileInput: ' + fileInput);

// files is a FileList object (similar to NodeList)
var files = fileInput.files;
console.log('files: ' + files);

But, I'm getting an error msg in the console log that files variable is undefined. 但是,我在控制台日志中收到一条错误消息msg,该错误是文件变量未定义。

The HTML <input> tag, configured to be a file picker, puts the names of the files into an object. HTML <input>标记(配置为文件选择器)将文件名放入对象中。 The information in the object needs to somehow get passed to the .gs file. 对象中的信息需要以某种方式传递到.gs文件。 Because the file picker can choose multiple files, the code needs to process multiple files. 由于文件选择器可以选择多个文件,因此代码需要处理多个文件。 You could upload the files one after the other, looping through all of them one at a time, or somehow run asynchronous code. 您可以一个接一个地上传文件,一次遍历所有文件,或者以某种方式运行异步代码。 A .gs script can start uploading a second file before the first one is done. .gs脚本可以在第一个文件完成之前开始上传第二个文件。 It's faster to be uploading multiple files at the same time rather than having the code wait to upload the next file before the current one is done. 同时上传多个文件比让代码在完成当前文件之前等待上传下一个文件要快。

You can run a .gs script directly from a submit button: 您可以直接通过“提交”按钮运行.gs脚本:

<input type="button" value="Submit"
  onclick="google.script.run.withSuccessHandler(gsFunctionName).processForm(this.parentNode)" />

Or run JavaScript in an HTML tag, which then runs the .gs server side code: 或在HTML标记中运行JavaScript,然后再运行.gs服务器端代码:

<form onsubmit="fncMyClientSideCode(this)" id="idUploadForm">
  <input>
  <button type="submit" id="btnUpload">Upload Now!</button>
</form>

<script>
  window.fncMyClientSideCode = function(objArgPassed) {
     //my statements here
     google.script.run.withFailureHandler(onUploadFail)
      .withSuccessHandler(onRegSccss)
     .fncUploadFiles(objArgPassed);
  }
</script>

I'm showing a couple different ways to use the HTML just for general information. 我展示了几种用于一般信息的HTML使用方式。 You need to modify it for your use, test and debug it. 您需要对其进行修改以供使用,测试和调试。

That's just the beginning. 那仅仅是开始。 You need to know how to access the information in the object, write code to loop through each file, save it to a specific folder maybe. 您需要知道如何访问对象中的信息,编写代码以遍历每个文件,或者将其保存到特定文件夹。 There are lots of questions dealing with saving files to Google Drive with Apps Script. 有关使用Apps脚本将文件保存到Google云端硬盘的问题很多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM