Allow remote methods to accept uploaded files
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
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.
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.
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:
-
Install
body-parserdependency.$ npm install --save body-parser -
Configure the
rawparser by adding the following content toparsesection of yourserver/middleware.jsonfile:{ "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'},
});
};
@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
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
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.