Comments (4)
I see the problem: you've added a 'data' handler right after you called the async mkdirp. This causes data to be released immediately. Remove that and you should be fine.
from busboy.
What node version are you using?
from busboy.
node v0.10.16
expressjs 3.4.4
connect-busboy 0.0.1
may be problem is about synchronous or asynchronous,
when i use mkdirp in synchronous mode, it's work fine.
problem happened when i switch to asynchronous mode
This is my code.
var express = require('express');
var http = require('http');
var busboy = require('connect-busboy');
var fs = require('fs');
var mkdirp = require('mkdirp');
var path = require('path');
var inspect = require('util').inspect;
var app = express();
app.use(
busboy(
{
limits : {
fileSize : 10 * 1024 * 1024
}
}
));
app.get('/upload', function(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="filefield"><br />\
<input type="submit">\
</form>\
</body></html>');
});
app.post('/upload', function(req, res, next){
req.busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
console.log('File [' + fieldname +']: filename: ' + filename + ', encoding: ' + encoding);
var rootDir = path.dirname(require.main.filename);
var uploadDir = 'media/photos';
var saveTo = path.join(rootDir, uploadDir);
mkdirp(saveTo, function(err) {
if (err) {
console.error(err);
return next(err);
} else {
console.log('Save File..');
saveTo = path.join(saveTo, path.basename(filename));
file.pipe(fs.createWriteStream(saveTo));
}
});
file.on('data', function(data) {
console.log('File [' + fieldname + '] got ' + data.length
+ ' bytes');
});
file.on('end', function() {
console.log('File [' + fieldname + '] Finished');
});
});
req.busboy.on('field', function(fieldname, val, valTruncated, keyTruncated) {
console.log('Field [' + fieldname + ']: value: ' + inspect(val));
});
req.busboy.on('end', function() {
console.log('Done parsing form!');
res.send(200, 'OK');
});
req.pipe(req.busboy);
});
http.createServer(app).listen(4000);
from busboy.
Brian White
Thank you so much.
from busboy.
Related Issues (20)
- Problem on utf8 filename HOT 5
- Missing Content-Type HOT 7
- [email protected] SyntaxError: Unexpected token { HOT 1
- Busboy does not recognize `image/jpeg` without `filename` as files HOT 1
- Extracting Filename when Empty String fails
- need to get raw headers HOT 1
- busboy seems to read ahead too fast for many small (< 16KiB) files HOT 5
- Events order when parsing file HOT 6
- Typings for field name HOT 1
- File event has not been triggered with nuxt 3 HOT 5
- Unexpected end of form HOT 2
- File size unavalaible HOT 1
- Unusable uploaded files. HOT 1
- Unusable uploaded files (with the right module) HOT 1
- Error: Unexpected end of form HOT 6
- Error: Malformed urlencoded form HOT 2
- Unknown license vulnerability HOT 2
- How to run benchmarks for busboy? HOT 2
- Wrong mime type HOT 3
- Normal error we see when busboy is called more than once HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from busboy.