Package Exports
- @exio.tech/upload-multipart-image
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@exio.tech/upload-multipart-image) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
UploadMultipartImage
Description
A node.js module for handling uploaded images and text fields with multipart/form-data for express.js.
Requirements
- node.js -- v11.5.0 or newer
Usage
const path = require('path')
const uploadImage = require('@exio.tech/upload-multipart-image')
const express = require('express')
const ROOT_DIR = require.main.path
const app = express()
app.get('/', (req, res, next) => {
res.send(`
<html>
<head></head>
<body>
<form method="POST" enctype="multipart/form-data">
<input type="text" name="textfield"><br />
<input type="file" name="imagefield"><br />
<input type="submit">
</form>
</body>
</html>
`)
})
app.post('/', uploadImage({
imageFieldNames: 'imagefield', // OR multiple - ['image1', 'image2']
destination: (fileInfo) => path.join(ROOT_DIR, './images'),
filename: (fileInfo) => fileInfo.defaultFilename + '.png',
sharp: (fileInfo) => fileInfo.defaultSharp.rotate(140).resize(400, 400).png(),
}), (req, res, next) => {
res.send(req.file)
})
app.listen(8000, (err) => {
if (err) return console.log(err)
console.log('Server started successfully')
})If imageFieldNames is an array of convertables to strings, then files metadata will be provided with req.files, not req.file.
You can omit this property, set it to null or empty_array, then images will be simply thrown away, and middleware will act as simle text field handler.
Details see in TSDocs.