【问题标题】:Google drive api to upload file not workingGoogle drive api上传文件不起作用
【发布时间】:2018-01-17 06:18:54
【问题描述】:
function listFiles(auth) {
  var service = google.drive('v3');

var fileMetadata = {
  name: 'My Report4'
  // mimeType: 'text/plain'
//  'mimeType': 'application/vnd.google-apps.spreadsheet'
};
var media = {
  mimeType: 'text/plain',
  body: fs.createReadStream('photo4.txt')
};
service.files.create({
  auth: auth,
  resource: fileMetadata,
  media: media,
  fields: 'id'

}, function (err, file) {
  if (err) {
    // Handle error
    console.error(err);
  } else {
    console.log('File Id:', file.id);
  }
});
}

error :{ [Error: socket hang up] code: 'ECONNRESET' } .

我认为问题出在正文中:fs.createReadStream('photo4.txt');

如果我将其更改为body : 'ANy text',它将起作用。

【问题讨论】:

    标签: javascript google-drive-api google-api-js-client


    【解决方案1】:

    在我的环境中,您的脚本运行良好。比如试试下面这个修改过的脚本怎么样?

    var service = google.drive('v3');
    fs.readFile('photo4.txt', function(err, content){
        var fileMetadata = {
            'name': 'My Report4',
        };
        var media = {
            mimeType: 'text/plain',
            body: new Buffer(content, 'binary'),
        };
        service.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);
            }
        });
    });
    

    如果这对您的环境没有用处,我很抱歉。

    【讨论】:

      猜你喜欢
      • 2017-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-10
      • 1970-01-01
      • 1970-01-01
      • 2020-03-07
      相关资源
      最近更新 更多