0

通常、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);
  });

},

ファイルをサーバーに保存せずに、クライアントから直接 POST するにはどうすればよいですか?

私は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);

});
4

1 に答える 1

-1

restlerデータ オプションの作成を支援するために、によってエクスポートされる 2 つのヘルパー関数があります。どちらも使用せず、JS オブジェクト リテラルを指定します。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);
于 2015-02-15T09:56:05.583 に答える