无法将多个文件上传到驱动器

时间:2019-04-17 04:59:06

标签: javascript google-apps-script filereader

我正在尝试通过多个文件上传按钮(6个按钮)在Google驱动器上上传多个文件。如果为每个按钮选择一个文件,我的代码将起作用。但是,当我尝试上传多个文件时,它给我一个错误 TypeError:FileReader.readAsDataURL的参数1没有实现接口Blob

script.html

  function uploadFiles() {
        var allFiles = [];
        allFiles.push(document.getElementById('myFiles1').files);
        allFiles.push(document.getElementById('myFiles2').files);
        allFiles.push(document.getElementById('myFiles3').files);
        allFiles.push(document.getElementById('myFiles4').files);
        allFiles.push(document.getElementById('myFiles5').files);
        allFiles.push(document.getElementById('myFiles6').files);
        //console.log(allFiles.length);
        //console.log(allFiles);
        var courseName = document.getElementById('cno').value;
        if (!courseName) {
            window.alert('Missing course name!');
        }
        var professorName = document.getElementById('pname').value;
        if (!professorName) {
            window.alert('Missing professor name!');
        }
        var folderName = 'XXX' + courseName + ' ' + professorName;
        if (allFiles.length == 0) {
            window.alert('No file selected!');
        } else {
            numUploads.total = allFiles.length;
            google.script.run.withSuccessHandler(function(r) {
                // send files after the folder is created...
                for (var i = 0; i < allFiles.length; i++) {
                    // Send each file at a time
                    uploadFile(allFiles[i], r.folderId);
                }
            }).createFolder(rootFolderId, folderName);
        }
    }

  function uploadFile(file, folderId) {
        var reader = new FileReader();
        reader.onload = function(e) {
            var content = reader.result;             
            google.script.run.withSuccessHandler(showSuccess)
                    .uploadFiletoDrive(content, file.name, folderId);
        }
        reader.readAsDataURL(file);
        document.getElementById('Assessment-form').reset();
    }

code.gs

function createFolder(parentFolderId, folderName) {
try {
    var parentFolder = DriveApp.getFolderById('MY_FOLDER_ID');
    var folders = parentFolder.getFoldersByName(folderName);
    var folder;
    if (folders.hasNext()) {
        folder = folders.next();
    } else {
        folder = parentFolder.createFolder(folderName);
    }
    return {
        'folderId' : folder.getId()
    }
} catch (e) {
    return {
        'error' : e.toString()
    }
}
}

function uploadFiletoDrive(base64Data, fileName, folderId) {
try {
    var splitBase = base64Data.split(','), type = splitBase[0].split(';')[0]
            .replace('data:', '');
    var byteCharacters = Utilities.base64Decode(splitBase[1]);
    var ss = Utilities.newBlob(byteCharacters, type);
    ss.setName(fileName);

    var folder = DriveApp.getFolderById(folderId);
    var files = folder.getFilesByName(fileName);
    var file;
    while (files.hasNext()) {
        // delete existing files with the same name.
        file = files.next();
        folder.removeFile(file);
    }
    file = folder.createFile(ss);
    return {
        'folderId' : folderId,
        'fileName' : file.getName()
    };
} catch (e) {
    return {
        'error' : e.toString()
    };
}
}

1 个答案:

答案 0 :(得分:1)

  

document.getElementById('Assessment-form').reset();

您似乎在每次上传后都重置了表单,而不是在第六次上传后就重置了表单。第一次上传后,allFiles个文件可能会丢失。

  

allFiles.push(document.getElementById('myFiles1').files);

您要推送filelist而不是file。试试

allFiles.push(document.getElementById('myFiles1').files[0]);

或者,您不需要使用filereader。您可以upload the form node directly。文件将自动转换为服务器上的Blob。