Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blank Data when Uploading Form Using enctype="multipart/form-data"

I have an interesting problem. I'm trying to upload a form by way of

<form enctype="multipart/form-data" action="/myendpoint/:id">
    <input type="hidden" name="data" value="mydata" />
    <input type="file" name="formname" />
</form>

... and my remote method call:

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'}},
           ]
  }
);

Unfortunately the body is coming in as blank

How do I get the form data? I modified server/datasources.json to include

"storage": {
    "name": "storage",
    "connector": "loopback-component-storage",
    "provider": "filesystem",
    "root": "./server/storage"
}

Still nothing.

Thanks

like image 305
Paul Avatar asked Dec 21 '25 18:12

Paul


1 Answers

So unfortunately the documentation is very limited when discussing how to upload files. There is one reference to the module "loopback-component-storage", one has to tear into it in order to find this diamond in the rough.

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'}
    }
);

You can find the documentation for StorageService here

like image 167
Paul Avatar answered Dec 24 '25 12:12

Paul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!