loopback icon indicating copy to clipboard operation
loopback copied to clipboard

Allow remote methods to accept uploaded files

Open bajtos opened this issue 9 years ago • 26 comments

Allow remote method to accept uploaded file(s) via regular "accepts" parameters.

So far, methods accepting files have to parse them manually from the request object, see loopback-component-storage for an example implementation.

Possible flavours to support:

  • Access the full request as a single "file" (see e.g. https://github.com/strongloop/strong-remoting/issues/318): {arg: 'file', type: 'file', http: { source: 'body' }, required: true}

  • Accept multiple files uploaded in a multipart body (see e.g. https://github.com/strongloop/loopback/issues/3264 and https://github.com/strongloop/loopback/issues/3171), this should simplify loopback-component-storage implementation

bajtos avatar Mar 09 '17 12:03 bajtos

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Apr 17 '18 00:04 stale[bot]

This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.

stale[bot] avatar May 01 '18 01:05 stale[bot]

Access the full request as a single "file" (see e.g. strongloop/strong-remoting#318):

{arg: 'file', type: 'file', http: { source: 'body' }, required: true}

Cross-posting my StackOverflow answer https://stackoverflow.com/a/53726432/69868. Please up-vote it if you find it useful.

I am recommending the following solution:

First, configure your server middleware to parser image request bodies:

  1. Install body-parser dependency.

     $ npm install --save body-parser
    
  2. Configure the raw parser by adding the following content to parse section of your server/middleware.json file:

     {
       "body-parser#raw": {
         "limit": "100kb",
         "type": "image/*"
       }
     }
    

    The "limit" option sets the maximum allowed request body size. You don't want to allow arbitrary size to prevent malicious clients from crashing your server on "out of memory" error.

    The "type" option configures content-types that should be parsed by this middleware. In my example above, I am allowing all image types.

Next, implement a remote method accepting the request body. Thanks to the raw body parser, the body stream will be already converted into a Buffer for you. In my example below, I have a simple method that responds with a base64-encoded body.

module.exports = function(Image) {
  Image.analyze = async function(data) {
    // Reject non-image requests, e.g. JSON
    if (!Buffer.isBuffer(data)) {
      const err = new Error('Unsupported media type'); 
      err.statusCode = 415;
      throw err;
    }

    // data is a Buffer containing the request body
    return data.toString('base64');
  };
    
  Image.remoteMethod('analyze', {
    accepts: [
      // {source: 'body'} is the important part
      {arg: 'data', type: 'object', http: {source: 'body'}},
    ],
    returns: {root: true, type: 'string'},
    http: {path: '/analyze', verb: 'post'},
  });
};

bajtos avatar Dec 11 '18 14:12 bajtos

@Gomathivignesh @JohnHour89 Can you share your solution? @bajtos Any updates to get files with multipart/form-data?

I'm using loopback v3. I am trying to upload an image with json form data. https://github.com/strongloop/loopback-component-storage/issues/237#issuecomment-598296893

pbalan avatar Mar 13 '20 07:03 pbalan

Have you looked at this LB3 example?

https://github.com/strongloop/loopback-example-storage/tree/master/example-3.0

Here is a LB 4 solution from a community member: https://stackoverflow.com/questions/54663283/how-to-use-storage-components-in-loopback-4-0/55213810#55213810

emonddr avatar Mar 13 '20 14:03 emonddr

Hi @emonddr, thanks for your reply. Are you referring to how to use loopback-component-storage? I have adapted several solutions to form solution for my problem. I'll post the solution to the above link once I complete my fix.

pbalan avatar Mar 15 '20 03:03 pbalan