Skip to content
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

Added limit file size example to Readme #231

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,56 @@ http.createServer(function(req, res) {
// Done parsing form!
```

* Limit file size

```javascript
var http = require('http'),
path = require('path'),
os = require('os'),
fs = require('fs');

var Busboy = require('busboy');

http.createServer(function(req, res) {
if (req.method === 'POST') {
var busboy = new Busboy({ headers: req.headers , limits: {fileSize : 1024 * 1024 }}); //fileSize is expressed in bytes. 1024*1024 = 1MB

busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
var saveTo = path.join(os.tmpdir(), path.basename(fieldname));
const stream = fs.createWriteStream(saveTo);
file.pipe(stream);

file.on('limit', function() {
res.writeHead(413, { 'Connection': 'close' });
res.end();
});

/*
Here is another use case:

Stream close event will be triggered when file size limit is reached. Busboy's stream will contain a boolean property called truncated:

stream.on('close', function() {
if (file.truncated){
...
}
});

*/

});
busboy.on('finish', function() {
res.writeHead(200, { 'Connection': 'close' });
res.end("That's all folks!");
});
return req.pipe(busboy);
}
res.writeHead(404);
res.end();
}).listen(8000, function() {
console.log('Listening for requests');
});
```

API
===
Expand All @@ -171,7 +221,7 @@ Busboy (special) events

* **file**(< _string_ >fieldname, < _ReadableStream_ >stream, < _string_ >filename, < _string_ >transferEncoding, < _string_ >mimeType) - Emitted for each new file form field found. `transferEncoding` contains the 'Content-Transfer-Encoding' value for the file stream. `mimeType` contains the 'Content-Type' value for the file stream.
* Note: if you listen for this event, you should always handle the `stream` no matter if you care about the file contents or not (e.g. you can simply just do `stream.resume();` if you want to discard the contents), otherwise the 'finish' event will never fire on the Busboy instance. However, if you don't care about **any** incoming files, you can simply not listen for the 'file' event at all and any/all files will be automatically and safely discarded (these discarded files do still count towards `files` and `parts` limits).
* If a configured file size limit was reached, `stream` will both have a boolean property `truncated` (best checked at the end of the stream) and emit a 'limit' event to notify you when this happens.
nerycordova marked this conversation as resolved.
Show resolved Hide resolved
* If a configured file size limit was reached, `stream` will have a boolean property `truncated` (best checked at the end of the stream) and emit a 'limit' event to notify you when this happens.

* **field**(< _string_ >fieldname, < _string_ >value, < _boolean_ >fieldnameTruncated, < _boolean_ >valueTruncated, < _string_ >transferEncoding, < _string_ >mimeType) - Emitted for each new non-file field found.

Expand Down