使用Node.js上传Ajax文件


                    

我正在尝试使用ajax文件上传插件(文件上传器插件)上传一个 文件到node.js服务器。 这是初始化插件的客户端代码:</ p>

  $(function(){
     / 准备就绪 /
   var uploader = new qq.FileUploader({
     //传递dom节点(对于jQuery用户,例如$(selector)[0])
     元素:document.getElementById('uploader'),
     //服务器端上传脚本的路径
     动作:“ / newitem”,
     allowedExtensions:['jpg','jpeg','png','gif'],
     上限:10000000
});
});
</ code> </ pre>

我的服务器代码是</ p>

  app.post('/ newitem',function(req,res){
       if(req.xhr){
         console.log('正在上传...');
         var fName = req.header('x文件名');
         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('全部完成!!!!');
             res.writeHead(200,{'Content-Type':'text / html'});
             重发();
         });
     }

         });
</ code> </ pre>

我的问题是我无法更新进度,并且上传器在上传成功时给出了失败的上传结果。 我认为这与ajax服务器响应有关,对吗? 我该如何解决?</ p>

谢谢</ p>
     </ div>

展开原文

原文

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个回答


更改</ p>

  req.on('end',function(){
       res.writeHead(200,{'Content-Type':'application / json'});
       res.end(JSON.stringify({
           成功:正确
       }));
   });
</ code> </ pre>
     </ div>

展开原文

原文

Change it with

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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐