使用enctype ="multipart/form-data"上传表单时的空白数据

Pau*_*aul 3 strongloop loopbackjs

我有一个有趣的问题.我正在尝试通过上传表单

<form enctype="multipart/form-data" action="/myendpoint/:id">
    <input type="hidden" name="data" value="mydata" />
    <input type="file" name="formname" />
</form>
Run Code Online (Sandbox Code Playgroud)

...和我的远程方法调用:

Patient.uploadVideo = function(id, mydata, cb) {
    console.log(mydata);
    return cb(null, { id: 123 });
};


MyModel.remoteMethod(
'uploadVideo',
{
  http: {path: '/:id/recording/:recordingid/videos', verb: 'post'},
  accepts: [
            {arg: 'id', type: 'string', required: true},
            {arg: 'mydata', type: 'object', 'http': {source: 'body'}},
           ]
  }
);
Run Code Online (Sandbox Code Playgroud)

不幸的是身体是空白的

如何获取表单数据?我修改了server/datasources.json来包含

"storage": {
    "name": "storage",
    "connector": "loopback-component-storage",
    "provider": "filesystem",
    "root": "./server/storage"
}
Run Code Online (Sandbox Code Playgroud)

依然没有.

谢谢

Pau*_*aul 5

所以遗憾的是,在讨论如何上传文件时,文档非常有限.有一个参考模块"loopback-component-storage",为了在粗糙中找到这颗钻石,必须撕下它.

var storage     =   require('loopback-component-storage');

MyModel.myFunction = function(req, res, options, cb) {
    var storage     =   require('loopback-component-storage');
    var storageService  = storage.StorageService({provider: 'filesystem', root: '/tmp'});

    storageService.upload(req, res, { container: 'upload' }, function(err, data) {
        console.log(data); // this provides a nice object with all of the variables and data wrt the file that was uploaded
        /// ..etc
    });
 };
 MyModel.remoteMethod(
    'myFunction',
    {
        http: {path: '/mypath', verb: 'post'},
        accepts: [
          {arg: 'req', type: 'object', 'http': {source: 'req'}},
          {arg: 'res', type: 'object', 'http': {source: 'res'}}
        ],
        returns: {arg: 'something', type: 'object'}
    }
);
Run Code Online (Sandbox Code Playgroud)

您可以在此处找到StorageService的文档