我正在尝试使用ajax文件上传插件(file uploader plugin)将文件上传到node.js服务器。这是我初始化插件的客户端代码:
$(function() {
/* dom ready */
var uploader = new qq.FileUploader({
// pass the dom node (ex. $(selector)[0] for jQuery users)
element: document.getElementById('uploader'),
// path to server-side upload script
action: '/newitem',
allowedExtensions: ['jpg', 'jpeg', 'png', 'gif'],
sizeLimit: 10000000
});
});
我的服务器代码是
app.post('/newitem',function(req, res) {
if(req.xhr) {
console.log('Uploading...');
var fName = req.header('x-file-name');
var fSize = req.header('x-file-size');
var fType = req.header('x-file-type');
var ws = fs.createWriteStream('./'+fName)
req.on('data', function(data) {
console.log('DATA');
ws.write(data);
});
req.on('end', function() {
console.log('All Done!!!!');
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end();
});
}
});
我的问题是我无法更新进度,上传器在上传成功时上传失败的结果。我认为这与ajax服务器响应有关我是对的吗?我怎么能解决它?
由于
答案 0 :(得分:1)
用
更改它 req.on('end', function() {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
success: true
}));
});