我在關注本教程:http://vimeo.com/17442755。 它教你如何使用node.js上傳文件。node.js中的文件上傳只是無限期地上傳?
這是一個完整的代碼在app.js:
var express = require('express');
var multipart = require('./multipart');
var fs = require('fs');
var app = express.createServer();
app.configure(function() {
app.use(express.logger());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.static(__dirname + '/static'));
});
app.configure('development', function() {
app.use(express.logger());
app.use(express.errorHandler({
dumpExceptions: true,
showStack: true
}));
});
app.configure('production', function() {
app.use(express.errorHandler());
});
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.get('/', function(req, res) {
res.render('root');
});
var products = require('./products');
app.get('/products', function(req, res) {
res.render('products/index', {locals: {
products: products.all
}});
});
app.get('/products/new', function(req, res) {
res.render('products/new', {locals: {
product: req.body && req.body.product || products.new
}});
});
app.post('/products', function(req, res) {
var id = products.insert(req.body.product);
res.redirect('/products/' + id);
});
app.get('/products/:id', function(req, res) {
var product = products.find(req.params.id);
res.render('products/show', {locals: {
product: product
}});
});
app.get('/products/:id/edit', function(req, res) {
var product = products.find(req.params.id);
res.render('products/edit', {locals: {
product: product
}});
});
app.put('/products/:id', function(req, res) {
var id = req.params.id;
products.set(id, req.body.product);
res.redirect('/products/'+id);
});
/* Photos */
app.get('/photos/new', function(req, res) {
res.render('photos/new');
});
app.post('/photos', function(req, res) {
req.setEncoding('binary');
var parser = multipart.parser();
parser.headers = req.headers;
parser.onPartBegin = function(part) {
ws = fs.createWriteStream(__dirname + '/static/upload/photos/' + part.filename);
ws.on('error', function(err) {
throw err;
});
};
parser.onData = function(data) {
ws.write(data);
};
parser.onPartEnd = function() {
ws.end();
parser.close();
res.send('File sucessfully uploaded.');
};
req.on('data', function(data) {
parser.write(data);
});
});
app.listen(4000);
現在,當我嘗試上傳文件,瀏覽器只是不停地裝載到永遠。
任何建議來解決這個問題?