-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
File upload with multipart/form-data #1873
Comments
Hi @diegolacarta, thank you for starting this discussion! Incidentally, @raymondfeng @marioestradarosa do you have any opinions on the implementation? Few questions that comes to my mind:
|
Thanks for the answer @bajtos , is there any way to plug that code into loopback in the mean time? |
I recommend to use https://github.com/expressjs/multer. |
We probably should implement a custom storage engine based on the controller method, allowing injections of fields/files to the method arguments. For example, export class FileUploadController {
upload(@file({name: 'my-file}) file: File) {
// ...
}
}
export class FileUploadController {
upload(
@file.handle() handle: (file: File) => Promise<void>, // handle a file
@file.remove() remove: (file: File) => Promise<void> // clean up a file
) {
// ...
}
} |
@strongloop/sq-lb-apex @hacksparrow @bajtos , are we good with proposal from @raymondfeng above? |
I believe |
+1 for |
For better or worse, the pull request #1880 is showing how to implement support for file uploads using As I see it, adding support for file uploads is a big feature that will live with us for many months or years and will be difficult to significantly change. I'd like us to carefully design this figure in a way that will be robust, secure and extensible to support future needs. A question for all of us to consider: should we implement file-upload as a built-in feature in Having file-uploads outside of Another concern to consider: export class FileUploadController {
upload(
@file.handle() handle: (file: File) => Promise<void>, // handle a file
@file.remove() remove: (file: File) => Promise<void> // clean up a file
) {
// ...
}
} Let's not use multiple method arguments for a single endpoint parameter ("files") please, it is difficult to extend in the future. I'd prefer to group export class FileUploadController {
upload(
@multipart() multipart: MultipartRequestBody,
) {
multipart.handle();
multipart.remove();
// ...
}
} |
Ping @strongloop/loopback-next. In this GitHub issue, we are discussing how to best implement support for receiving file uploads in LB4 applications. Please let us know if this is something you are interested in. If you are just interested in seeing the feature implemented, then upvote this issue by pressing the thumb-up button just below the issue description. If you can help us with relevant information or experience that can make our implementation better, then please leave a comment. |
As a developer that needs to create a REST API to accept file uploads, I would like to implement the following minimum logic:
Please note a A possible style: Use export class FileUploadController {
@post('/uploads')
@upload({
limits: {...},
fileFilter: ...,
storage: ...,
})
upload(
@upload.files() files: File[], @upload.fields([
{ name: 'avatar', maxCount: 1 },
{ name: 'gallery', maxCount: 8 }
]) fields: Field[]) {
// return a response;
}
)
} |
What about even simpler solution - 5c2051d? Basically, we allow controller methods to skip body parsing by configuring |
I like this idea as a short-term workaround allowing LB4 app developers to build their own solutions for parsing multipart/form-data requests, buying us more time to implement the official support in a robust way. I am proposing the following approach:
|
It would be great if @raymondfeng WDYT? |
@bajtos anyway, can you have an example of controller upload file? |
@thanhdevapp The pull request #1936 is adding features that will allow applications & extensions to implement custom request parsing (e.g. using multer), see the following section for information about implementing a file-upload controller: Parsing-requests.md#specify-custom-parser-by-controller-methods. Please not this pull request was not landed yet, these new APIs are not available yet. |
FYI, the extensible body parsing is available, you can learn more in Raymond's blog post here: https://strongloop.com/strongblog/the-journey-to-extensible-request-body-parsing/ While working on #1936, we were discussing built-in support for file uploads too. Our conclusion was that there are different styles of consuming uploaded files and it would be too difficult to build a single component to support them all. For example, some applications may want to save the uploaded files into a temporary directory for further processing. Other applications may want to stream the uploaded files directly to a backed storage (e.g Amazon S3 or IBM Cloud Object Storage). @raymondfeng in the light of that decision, which of the following acceptance criteria are still relevant and which should be removed?
Personally, I think we should have an example application (LB4 server + HTML5 front-end) to show file uploads in practice and to validate our proposed solution in a real-world setup (end-to-end, from the browser). |
+1 for a LB4 server + HTML5 front-end Example Repo |
There is documentation showing how to do file upload: https://loopback.io/doc/en/lb4/Parsing-requests.html#specify-custom-parser-by-controller-methods. What we might be able to do is to:
|
Updated Acceptance Criteria:
|
@raymondfeng created a PR #4801 to add an example. |
some update: PR #4801 merged. Next we need update the docs mentioned in #1873 (comment) |
One more PR for further improvement - #4882 |
How can we upload file (Single and Multiple) |
@VishalSingh28 You will have to follow https://github.com/expressjs/multer/blob/master/StorageEngine.md to create a multer storage engine for MySQL and configure the file uploader to use it. See an example at https://github.com/strongloop/loopback-next/blob/master/examples/file-transfer/src/application.ts#L59. |
Is there any way to handle an upload with multipart/form-data? I'm trying to migrate a simple piece of code that works in express to loopback, and can't find any docs for v4.
The issue is it never gets to the controller, and returns an error saying multipart/form-data is not supported.
This is the code in expressjs:
Acceptance criteria
@loopback/rest
allowing applications & extensions to provide custom request body parsers. See feat(rest): switch to body parser and add extensibility for parsers #1936examples
directory) showing file uploads in practice, using the official extension on the server and providing a simple HTML-based UI on the client side.multipart/form-data
from REST API Explorer #2666The text was updated successfully, but these errors were encountered: