【问题标题】:Is it possible to send a file from a client to restler.file?是否可以将文件从客户端发送到restler.file?
【发布时间】:2015-02-04 08:02:57
【问题描述】:

通常 restler.post() 需要一个 restler.file(...) 对象来发送一个 POST。如果您在服务器上有文件路径,则此方法有效。

例如:

//#############################################################################
// Video upload from file system on node console server
//#############################################################################

function(done){

  var fileStats = fs.statSync(videoFileName);
  var fileSizeInBytes = fileStats["size"];

  var data = {
    'name': objectVideoName,
    'type':'video',
    'content': 'application/mp4',
    'file': restler.file(videoFileName, null, fileSizeInBytes, null, "video" )
  };


  restler.post('https://api.astra.io/v0/bucket/'+bucketName+'/object', {
    multipart: true,
    data   : data,
    headers : { "Astra-Secret": astraSecret }
  }).on('complete', function(response) {
    console.log('Upload video to bucket: \n', response);
    done(null);
  });

},

如何直接从客户端发布文件而不将其保存在服务器上?

我正在尝试与 busboy 合作。

//#############################################################################
// Video upload from client then POST to cdn
//#############################################################################
app.post('/addVideo', function (req, res) {

var video = {};
var busboy = new Busboy({headers: req.headers});

busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {

    var ext = mimetype.split('/').pop();
    var token = generateToken(crypto);

    var fileSizeInBytes = 900000;

    filename = token + ext;

    var videoName = filename;

    var bucketName = 'sampleVideos';

    var data = {
      'name': videoName,
      'type':'video',
      'content': 'application/mp4',
      'file': ***** WHAT GOES HERE ******
    };

    restler.post('https://api.astra.io/v0/bucket/'+bucketName+'/object', {
      multipart: true,
      data   : data,
      headers : { "Astra-Secret": astraSecret }
    }).on('complete', function(response) {
      console.log('Upload video to bucket: \n', response);
      res.send("200");
    });

});

busboy.on('field', function (fieldname, val, fieldnameTruncated, valTruncated) {
    video[fieldname] = val;
});

busboy.on('finish', function () {
    console.log('busboy finished');
});

req.pipe(busboy);

});

【问题讨论】:

    标签: node.js express restler busboy


    【解决方案1】:

    restler 导出了两个辅助函数来帮助创建数据选项。您不使用任何一个并指定 JS Object 文字。 restler.data() 是你想用的

    data: function(filename, contentType, data) {
        return new Data(filename, contentType, data);
    }
    

    所以不是

    var data = {
      'name': videoName,
      'type':'video',
      'content': 'application/mp4',
      'file': ***** WHAT GOES HERE ******
    };
    

    在您的示例中,您可以这样做,因为它允许您使用任意内容而不是文件名

    var data = restler.data(videoName, 'application/mp4', file);
    

    【讨论】:

    • 问题已经包含了。问题是如何将流从 busboy 提供给 restler。
    • 不,您使用的是 JS 对象字面量。我会更新答案,让答案更清楚。
    • 您可能还需要等待file.on('data', function(incomingData){}) 真正获得文件的内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-25
    • 2015-05-02
    • 2010-11-04
    • 2018-10-27
    相关资源
    最近更新 更多