Skip to content

Commit

Permalink
docs: better handle parsing errors in file name
Browse files Browse the repository at this point in the history
  • Loading branch information
GrosSacASac committed Feb 11, 2021
1 parent 557b504 commit 22de063
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ file system.
form.on('fileBegin', (formName, file) => {
// accessible here
// formName the name in the form (<input name="thisname" type="file">)
// file.name (http filename)
// file.name http filename or null if there was a parsing error
// file.path default pathnme as per options.uploadDir and options.filename
// file.path = CUSTOM_PATH // to change the final path
});
Expand Down
11 changes: 11 additions & 0 deletions examples/with-http.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,18 @@ const server = http.createServer((req, res) => {
// parse a file upload
const form = formidable({ multiples: true });

form.on("fileBegin", (formName, file) => {
if (file.name === null) {
file.name = "invalid-characters"
}
})
form.parse(req, (err, fields, files) => {
if (err) {
console.error(err);
res.writeHead(400, { 'Content-Type': 'text/plain' });
res.end("Bad Request");
return;
}
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ fields, files }, null, 2));
});
Expand Down

0 comments on commit 22de063

Please sign in to comment.