4

This is a continuation from How to Use Advanced Drive Service to Upload Files.

My Webapp consists of an upload form for data files, which are then stored on Google Drive. (Full code in snippet below.) I'm having a problem with the following line of code:

    var file = Drive.Files.insert(resource, mediaData); // create file using Drive API

    try {  
      
        //Get root folder and pull all existing folders, plus setup variables pulled from form   
        var dropbox = form.Country;
        var timeStamp = new Date();      
        
        //Set file name slightly differently for Weekly Member Report (do not want to overright based on name just keep each extract so add timestamp to name)
        if (form.reportType == "Member Weekly"){
          var filename = form.reportType + timeStamp + ".xls";
        }
        else 
        {
        var filename = form.reportType+".xls";
        }
      
        var rootfolder = DriveApp.getFolderById("0Byvtwn42HsoxfnVoSjB2NWprYnRiQ2VWUDZEendNOWwwM1FOZk1EVnJOU3BxQXhwU0pDSE0");
        //Note root folder is Live Uploads Folder in Flatworld App folder structure
        var folder, folders = rootfolder.getFoldersByName(dropbox);
        
        //Check if folder exists and if not create    
        if (folders.hasNext()) {
          folder = folders.next();
        } else {
          folder = rootfolder.createFolder(dropbox);
        }
      
        var FolderURL = folder.getUrl(); // Retain URL of folder for final end message to user
      
        //Check if file already exists and delete if it does
        var file, files = folder.getFilesByName(filename);
        while( files.hasNext()){
          file = files.next();
          file.setTrashed(true);
        }
        
      //New Code from Stackover Flow:
      
      //Upload file and set various properties
      var mediaData = form.myFile;    
      var resource = {
        title: filename,
        mimetype: 'application/vnd.ms-excel',
        description: "Uploaded via BNI Upload Form by " + form.myName + " on: " + timeStamp
      };
      var file = Drive.Files.insert(resource, mediaData); // create file using Drive API
      var fileId = file.id;
      var DriveAppFile = DriveApp.getFileById(fileId); // retrieve file in DriveApp scope.
      var FileURL = DriveAppFile.getUrl(); //Retain URL of file for final end message to user
      
      DriveApp.removeFile(DriveAppFile); // remove new file from Users root My Drive
      folder.addFile(DriveAppFile); // puts file in selected folder
      
      
      //End of New code from Stackover Flow
       
        //Success message displayed to user    
      return "Thanks! File uploaded successfully to: <br><br><b>Folder Location:</b> " + FolderURL + "<br>" + "<b>File:</b> " + FileURL + ". <br><br>For any queries please email user@example.com copying the URLs displayed here in your email. You can close this browser window now or use the link below to upload another file.<br><br>";
        
      } catch (error) {
        
        //Catch error return it to user and email with error details

Its throwing the error message "Empty Response" on the line of code above when we try and upload a large file (15MB) Do you have any suggestions. This is well inside the Files insert limit of 5120GB and the code works fine on smaller files.

I've now tried to add in a loop in function to try the upload a couple of times, still throwing the same error sadly:

  //setup function that will return null if file is not uploaded correctly
  function createDriveFile(resource_f, mediaData_f){
    try{ 
      var file = Drive.Files.insert(resource_f, mediaData_f); // create file using Drive API
      return file;
    } catch (e) {return null;}
  }
  
  
  //try upload and loop if null
  var maxTries = 3;
  var tries = 0;
  do {
    tries = tries + 1;
    if (tries > 0) {
     var file = createDriveFile(resource, mediaData);
     Logger.log("I'm trying to upload, try number: " + tries);
    }
  } while ((file == null) && (tries < maxTries));
  if (file == null) {
  var file = Drive.Files.insert(resource, mediaData); // try one laste time to create file using Drive API - outside loop function so if error is thrown script stops
  }

The error only seems to occur on a larger file, even if we reduce the size of the same file that solves error so do we need to adjust the upload process to account for a larger file. Is there a Google Apps Script equivalent of making the API upload request resumable?

Community
  • 1
  • 1
astwood
  • 85
  • 1
  • 7
  • `timestamp` in your script is a date object. It will need formatting as a string in some form to append to other strings. Might be unrelated to your issue. – JSDBroughton Jul 13 '15 at 17:49
  • 1
    I'd put a `Logger.log('mediaData: ' + mediaData);` statement right after the line that assigns the `mediaData` variable, then View the Logs, and see what it states. – Alan Wells Jul 13 '15 at 18:36
  • @Jonathon - `timestamp` will be coerced to a string because the first argument in the addition, `form.reportType`, is a string. It might not _look_ exactly the way the OP wants, but it will be a string. (Could use [`Utilities.formatDate()`](https://developers.google.com/apps-script/reference/utilities/utilities.html#formatDate(Date,String,String)) if concerned.) – Mogsdad Jul 13 '15 at 19:19
  • @SandyGood Added the `Logger.log("mediaData: " + mediaData);` and got the following output to the log: _[15-07-14 06:46:14:706 BST] mediaData: FileUpload_ which is what I'd expect as its a file were uploading? – astwood Jul 14 '15 at 05:48
  • @Jonathon Quick question could it be with a bigger file that the file upload type needs to be set to [resumable](https://developers.google.com/drive/web/manage-uploads#resumable) so that it doesn't get interupted? – astwood Jul 14 '15 at 07:17
  • Hi Guys, quick update error does not show if we shrink file size, it seems to all be due to larger file size – astwood Jul 14 '15 at 07:41

2 Answers2

2

Your file size is the determinant factor here. Referencing the documentation suggests the simple upload method used here is good for up to 5MB only.

https://developers.google.com/drive/web/manage-uploads

Your comments seem to confirm this is what is happening for you.

As you hinted, use the resumable upload method. Use the uploadType: resumable parameter flag – API docs on the insert method describes how.

JSDBroughton
  • 3,966
  • 4
  • 32
  • 52
0

You can also check the naming of the file, for me I had a slash in the name which is why it would not upload. So take away any special characters before uploading and it should work.

Timisorean
  • 1,388
  • 7
  • 20
  • 30