1

我正在尝试使用 ajax 文件上传插件(文件上传插件)将文件上传到 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 服务器响应有关,对吗?我该如何解决?

谢谢

4

1 回答 1

1

改变它

   req.on('end', function() {
      res.writeHead(200, { 'Content-Type': 'application/json' }); 
      res.end(JSON.stringify({
          success: true
      }));
  });
于 2012-05-17T01:26:38.130 回答