插入文件时,高级驱动器服务返回空响应错误 [英] Advanced Drive Service returning Empty Response Error when inserting file

查看:93
本文介绍了插入文件时,高级驱动器服务返回空响应错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是的延续如何使用高级云端硬盘服务上传文件



我的Web应用包含一个数据文件的上传表单,然后存储在Google Drive上。 (完整的代码在下面的代码片段中)
我遇到了以下代码行的问题:

  var file = Drive.Files.insert(resource,mediaData); //使用Drive API创建文件

  try {//获取根文件夹并将所有现有文件夹,以及从表单拉出的设置变量var dropbox = form.Country; var timeStamp = new Date(); //为Weekly Member Report设置文件名称略有不同(不要基于名称进行重写,只保留每个提取文件,以便将时间戳添加到名称中)if(form.reportType ==Member Weekly){var filename = form.reportType + timeStamp +.xls; } else {var filename = form.reportType +。xls; } var rootfolder = DriveApp.getFolderById(0Byvtwn42HsoxfnVoSjB2NWprYnRiQ2VWUDZEendNOWwwM1FOZk1EVnJOU3BxQXhwU0pDSE0); //注意根文件夹是Flatworld App文件夹结构中的活动上传文件夹var folder,folders = rootfolder.getFoldersByName(dropbox); //检查文件夹是否存在以及是否创建if(folders.hasNext()){folder = folders.next(); } else {folder = rootfolder.createFolder(dropbox); } var FolderURL = folder.getUrl(); //保留用于最终结束消息的文件夹的URL //检查文件是否已经存在,并删除它是否存在var文件,files = folder.getFilesByName(filename); while(files.hasNext()){file = files.next(); file.setTrashed(真); } // Stackover Flow的新代码://上传文件并设置各种属性var mediaData = form.myFile; var resource = {title:filename,mimetype:'application / vnd.ms-excel',description:通过BNI上传表单上传+form.myName +on:+ timeStamp}; var file = Drive.Files.insert(resource,mediaData); //使用Drive API创建文件var fileId = file.id; var DriveAppFile = DriveApp.getFileById(fileId); //在DriveApp范围内检索文件。 var FileURL = DriveAppFile.getUrl(); //将用于最终结束消息的文件的URL保留给用户DriveApp.removeFile(DriveAppFile); //从用户root中删除新文件我的云端硬盘folder.addFile(DriveAppFile); //将文件放入所选文件夹// Stackover Flow的新代码结束//向用户显示成功消息返回谢谢!文件已成功上传到:< br>< br>< b>文件夹位置:< b>+ FolderURL +< br> +< b>文件:< / b>+ FileURL +。< br>< br>如有任何查询,请发送电子邮件至user@example.com复制您的电子邮件中显示的网址。浏览器窗口或使用下面的链接上传另一个文件。< br>< br>; } catch(error){//捕获错误将其返回给用户和带有错误细节的邮件 



当我们尝试上传大文件(15MB)时,它会在上面的代码行中抛出错误消息Empty Response(空响应)。您有任何建议。这完全在5120GB的文件插入限制内,并且代码可以在较小的文件上正常工作。



我现在试着添加一个函数循环来尝试上载几次,仍悲哀地抛出同样的错误:



  //设置函数,如果文件没有正确上传,将返回null函数createDriveFile(resource_f,mediaData_f){try {var file = Drive.Files.insert(resource_f,mediaData_f); //使用Drive API返回文件创建文件; } catch(e){return null;}} //尝试上传并循环,如果null var maxTries = 3; var tries = 0;做{试试=试+ 1;如果(尝试> 0){var file = createDriveFile(resource,mediaData); Logger.log(我试图上传,尝试编号:+尝试); ((文件== null)&&(尝试< maxTries)); if(file == null){var file = Drive.Files.insert(resource,mediaData); //使用Drive API创建文件的一段时间 - 外部循环函数,如果发生错误,脚本将停止}  



这个错误似乎只发生在一个更大的文件上,即使我们减少了解决错误的同一个文件的大小,我们也需要调整上传过程来解决更大的文件。是否有Google Apps脚本相当于将 API上传请求恢复为可用

解决方案

您的文件大小是决定因素。参考文档表明,这里使用的简单上传方法仅适用于高达5MB。

web / manage-uploadsrel =nofollow> https://developers.google.com/drive/web/manage-uploads



您的评论似乎证实这是你正在发生的事情。



正如你所暗示的那样,使用可恢复的上传方法。使用 uploadType:resumable 参数标志 - 插入方法的API文档描述了如何。


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?

解决方案

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.

这篇关于插入文件时,高级驱动器服务返回空响应错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆