2017-07-28 113 views
0

嘿,我發現這個v2解決方案可以獲取文件的上傳進度。 Get upload progress for Google Drive NodeJS client?Google Drive API v3請求進度bug

這裏是我的代碼

function real_upload_files(auth) { 
var drive = google.drive({ version: 'v3', auth: auth }); 

    fs.readFile('./test.zip', function(err, content){ 
     var fileMetadata = { 
      'name': 'test.zip', 
     }; 
     var media = { 
      mimeType: 'application/zip', 
      body: new Buffer(content, 'binary'), 
     }; 
     var req = drive.files.create({ 
      resource: fileMetadata, 
      media: media, 
      auth: auth, 
      fields: 'id', 
     }, function(err, file) { 
      if (err) { 
       console.log(err); 
      } else { 
       console.log('File Id: ', file.id); 
       clearInterval(q); 
      } 
     }); 

     var q = setInterval(function() { 
     console.log("Uploaded: " + req.req.connection.bytesWritten); 
     }, 250); 

    }); 
} 

但我只得到

上傳:428
上傳:428
上傳:428
上傳:428

的結果。任何想法爲什麼?

回答

0

我認爲上面的代碼ist只是錯誤的:D然而,這終於用我的Windows cmd。

function uploadFile_googledrive(auth) { 
var drive = google.drive({ version: 'v3', auth: auth }); 


     var fileMetadata = { 
      'name': 'box.box', 
      parents: [ folderID ] 

     }; 
     var media = { 
      mimeType: 'application/octet-stream', 
      body: fs.createReadStream('C:\\Exports\\box.box') 
     }; 
     var req = drive.files.create({ 
      resource: fileMetadata, 
      media: media, 
      auth: auth, 
      fields: 'id', 
     }, function(err, file) { 
      if (err) { 
       console.log('Error while uploading your Container to Google Drive - Error: ', err); 
      } else { 
       console.log('Successfull uploaded your Container to Google Drive with ID: ', file.id); 
       clearInterval(q); 
      } // else 
     }); // drive.files.create({ 


    var q = setInterval(function() { 
     console.log("Uploaded: " + req.req.connection.bytesWritten); 
    }, 1000); 


} // function uploadFile_googledrive() { 

父母:[folderID]爲folderID您需要從谷歌驅動器API的默認腳本做列表視圖。