【问题标题】:Valums file-uploader on nodejs - multiple file uploadnodejs 上的 Valums 文件上传器 - 多个文件上传
【发布时间】:2011-11-24 10:48:11
【问题描述】:

我正在使用valums ajax file-uploader

我的 nodejs 服务器端如下所示:

fileStream = fs.createWriteStream(__dirname+'/../public/images/houses/'+rndname);
req.pipe(fileStream);
req.on('end', function() {
  body = '{"success":"true", "name": "'+rndname+'"}';
  res.writeHead(200, 
    { 'Content-Type':'text/plain'
    , 'Content-Length':body.length
    });
  res.end(body);
});

客户端:

        function createUploader(){            
        var uploader = new qq.FileUploader({
            element: document.getElementById('homepic'),
            action: '/server/upload',
            allowedExtensions: ['jpg', 'png', 'gif'],
            multiple:true,
            onComplete: function(id, fileName, responseJSON){
              $("#homepic").append("<img src='/images/houses/"+responseJSON.name+"' class='mediumpic' />  ");
            }
        });           
    }
window.onload = createUploader;

这一切都适用于单个文件上传,非常棒!

所以想象一下 - 我按下上传按钮,选择图片,它上传非常快,显示在屏幕上。 现在我想上传另一个。我选择图片,它快速上传到服务器(我在服务器上看到它),我得到新文件名和成功的响应,我把图片和我的追加放在屏幕上。但是图片没有显示出来。我尝试在新标签中打开只是图片,即使我在服务器上看到它站在正确的目录中,仍然什么也没有。等待 3-5 分钟后,它就会出现,甚至不需要刷新页面。是什么导致了这种行为?是管道,我需要打电话给马里奥来修理它还是别的什么? :)

【问题讨论】:

标签: node.js file-upload


【解决方案1】:

将我的 req.pipe 更改为 req.on('data') 并开始工作。不知何故,我的 req.pipe 似乎没有关闭连接,并且“期待”即使所有文件都已上传,也会有更多数据出现。这就是为什么我不能调用 GET 来归档并且它处于“待处理”状态。 这解决了我的问题:

req.on('data', function(data) { 
      ws.write(data); 
    }); 
    req.on('end', function(data) { 
      var body = '{"success":"true", "name": "'+rndname+'"}';
      res.writeHead(200, 
        { 'Content-Type':'text/plain'
        , 'Content-Length':body.length
        });
      res.end(body);
    }); 

即使我找到了我的问题的解决方案,如果有人知道为什么 req.pipe 没有关闭连接并且在图片出现之前被卡住了 2-3 分钟,请告诉我。

【讨论】:

    【解决方案2】:

    我创建了一个 Express 3.x 中间件组件,允许您通过 req.files 集合访问 valuems/fineuploader 上传的文件。

    您需要做的就是在 Express 配置期间添加中间件,如下所示:

    var fineuploaderExpressMiddleware = require('fineuploader-express-middleware');
    
    app.use(express.cookieParser());
    app.use(express.bodyParser());
    app.use(fineuploaderExpressMiddleware({ uploadDir: '/tmp ' }));
    

    该组件在此处可用:https://github.com/suprememoocow/fineuploader-express-middleware

    【讨论】:

      猜你喜欢
      • 2012-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      • 2012-12-01
      • 2011-04-25
      • 1970-01-01
      相关资源
      最近更新 更多