You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Web应用FileReader数据传递至Google Apps Script客户端为空问题求助

多文件上传数据在Google Apps Script客户端返回空值/Undefined的问题

我开发的Web应用包含多个文件输入元素,支持用户上传任意数量、不同类型的文件。我尝试将所有数据先存储在与输入元素数量一致的数组中:数组的每个元素包含对应上传文件的文件名、blob、mimetype数据;若用户未上传文件,则通过data属性(header)保存该行的空上传引用。

数据在浏览器控制台中可见,但从Google Apps Script客户端访问时返回undefined/空值/Null。我对FileReader不太熟悉,服务器端需要将每个blob在Google Drive中创建文件,并最终将URL存储到表格中。相关代码如下:

function saveFormData(){

  var fileInputs = document.querySelectorAll('tr.result-row td > input.upload');
  var firstUploadIndex = fileInputs[0].parentElement.parentElement.getAttribute('data-index');

  //Loop through all upload rows
  var uploads = []; //will be array of arrays of multi-uploaded files; each item inside belongs to 1 column in db
  Array.from(fileInputs).forEach(input => {

    var files = input.files; //object file list

    if(files.length < 1){ //if there's no file in the upload input
      var header = input.getAttribute('data-uploadtype'); //save the header name as reference
      var fileData = [header, '', '', '']; 
      uploads.push(fileData);
    } else { //if there's a file/s in the upload input
      var innerUploads = []; //array of data for each file
      //Loop through all these files
      for(var i = 0; i < files.length; i++){
        
        var currentFile = files[i]; 

        if(!currentFile) return; //if no current file, just in case
        const fr = new FileReader();

        fr.onload = (e) => {
          var data = e.target.result.split(','); //split current file
          var fileData = { binary: data[1], mimeType: data[0].match(/:(\w.+);/)[1], filename: currentFile.name };
          innerUploads.push(fileData);
        }
        fr.readAsDataURL(currentFile);
      } //CLOSES FOR LOOP
      uploads.push(innerUploads);

    } //CLOSES IF
  
  }); //CLOSES FOR EACH LOOP

  google.script.run.saveToDrive(uploads);

} //CLOSES FUNCTION

上传界面如下:
表格单元格内的上传元素

内容的提问来源于stack exchange,提问作者Jed Santos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 15:25:34