Open
Description
- Used appropriate template for the issue type
- Searched both open and closed issues for duplicates of this issue
- Title adequately and concisely reflects the feature or the bug
Feature Request
Use Case
By adding the multipartFileHandler
or multipartHandler
to the bodyParser
options, the ordinary process of formidable does not continue (the form.handlePart(part)
would not be called anymore). So, we must write our handling function to save the incoming file for example.
server.use(restify.plugins.bodyParser({
// other options
// ...
multipartFileHandler: function (part) {
const {mime} = part;
const fileType: string = mime.split('/').pop();
if (fileType === 'jpg' || fileType === 'png' || fileType === 'jpeg') {
part.on('data', buffer => {
fs.writeFile(filePath, buffer, (err) => {
if (err) {
console.log(err);
} else {
console.log("The file was saved:", filename);
// form.emit('file', part.name, theSavedFile);
}
});
});
} else {
console.log('incorrect file type:', fileType);
}
},
// ...
});
Besides, this method leaves the request.files
to be an empty object {}
. As we can not call the form.emit('file', part.name, theSavedFile)
to ask the formidable: "the {[part.name]: theSavedFile}
was successfully received, please add it the the files
object".
Example API
We can easily run this snippet and save the file like a piece of cake.
server.use(restify.plugins.bodyParser({
multiples: true,
uploadDir,
multipartFileHandler: function (part, req, form) { // <-- the third argument can be added
const {mime} = part;
const fileType: string = mime.split('/').pop();
if (fileType === 'jpg' || fileType === 'png' || fileType === 'jpeg') {
form.handlePart(part); // <--- this line can be used.
} else {
console.log('incorrect file type:', fileType);
}
}
})
Are you willing and able to implement this?
It's not so hard. However, I wrote a snippet to inject "form" into the functions.
Metadata
Metadata
Assignees
Labels
No labels