Example application for file upload/download with LoopBack 4
npm install @loopback/example-file-transferAn example application to demonstrate file uploads and downloads for LoopBack 4
This application exposes POST /files endpoint that acceptsmultipart/form-data based file uploads. The uploaded files can be listed usingGET /files and individual files can be downloaded usingGET /files/.
- FileUploadController
- Expose POST /files endpoint to allow file uploads
- FileUploadService - an Express middleware from multer
- A service provider that returns a configured multer request handler
The file upload is configured with multer options in
src/application.ts as follows:
``ts.sandbox
// Configure file upload with multer options
const multerOptions: multer.Options = {
storage: multer.diskStorage({
// Upload files to `
destination: path.join(__dirname, '../.sandbox'),
// Use the original file name as is
filename: (req, file, cb) => {
cb(null, file.originalname);
},
}),
};
this.configure(FILE_UPLOAD_SERVICE).to(multerOptions);
- FileDownloadController
- Expose GET /files endpoint to list uploaded filesGET /files/{filename}
- Expose endpoint to download a file
Start the app:
`sh`
npm start
The application will start on port 3000. Open http://localhost:3000 in your
browser. You can try to upload a few files using the web UI or API explorer.
By default, the uploaded files will be stored in .sandbox folder under thefileStorageDirectory
application root directory. The directory can be configured via of application config.
Run npm test` from the root folder.
See
all contributors.
MIT