weixin_33738555 2012-05-09 18:01 采纳率: 0%
浏览 30

使用Node.js上传Ajax文件

I'm trying to use ajax file upload plugin(file uploader plugin) to upload a file to node.js server. This is my client side code to initialize plugin:

$(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
});
});

My server code is

  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();
        });
    }

        });

My problem is that I can't get the progress updated and the uploader gives failed result for uploads while upload success. I think this is related to ajax server response Am I right? How could I fix it?

Thanks

  • 写回答

1条回答 默认 最新

  • weixin_33670713 2012-05-17 01:26
    关注

    Change it with

       req.on('end', function() {
          res.writeHead(200, { 'Content-Type': 'application/json' }); 
          res.end(JSON.stringify({
              success: true
          }));
      });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算