用Sails.js 0.10和Skipper使用Dropzone.js上传多个文件 [英] Uploading multiple files with Sails.js 0.10 and Skipper using Dropzone.js

查看:313
本文介绍了用Sails.js 0.10和Skipper使用Dropzone.js上传多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的sails应用程序中有多个文件上传的问题。
我试图用Dropzone.js实现多个文件上传,后端是Sails v0.10.0-rc8。

现在当我通过dropzone上传一些文件时,我发现在多次上传的情况下,它会在请求中发送带有单独参数的文件。参数名称是'photo [0]','photo [1]','photo [2]'... ... 。我得到的控制器中的文件是这样的:
$ b $ pre $ req.file(file).upload(function(err,files){

//保存文件

});

但是,当有多个文件提交时,请求将在所有文件解析和存储请求,所以我只得到我的控制器中的一个文件。



有没有人遇到过这个问题?也许不支持多个文件上传与不同的请求参数在船长身体分析器?因为当我在一个属性('photo')中提交多个文件时,所有这些文件都被处理并传递给控制器​​。解析方案

这应该工作,只要你异步循环param的名字,例如:

  async.map(paramNames,function(file,cb ){

req.file(file).upload(function(err,files){

//保存文件,然后:
return cb err,files);

});

},函数doneUploading(err,files){

//如果发生错误,显示服务器错误
if(err){return res.serverError(err);}
//否则列出上传的文件
return res.json(files);

});

我能够成功地测试这个。


I have an issue with multiple file upload in my sails app. I am trying to implement multiple file upload with Dropzone.js and my backend is Sails v0.10.0-rc8.

Now when I upload some files via dropzone, I see that in case of multiple upload it sends the files with separate params in the request. The param names are 'photo[0]', 'photo[1]', 'photo[2]',.... I am getting the files in controller like this:

req.file(file).upload(function (err, files) {

    // save the file

});

But when there is more then one file submitted, the request is being passed to controller before all files are parsed and stored from request, so I get only one file in my controller.

Has anyone experienced this issue? Maybe there is no support for multiple file upload with different request parameters in skipper body parser? Because when I submit several files inside one attribute ('photo'), all of them are handled and passed to controller.

解决方案

This should work, provided you loop through the param names asynchronously, e.g.:

async.map(paramNames, function(file, cb) {

    req.file(file).upload(function (err, files) {

        // save the file, and then:
        return cb(err, files);

    });

}, function doneUploading(err, files) {

       // If any errors occurred, show server error
       if (err) {return res.serverError(err);}
       // Otherwise list files that were uploaded
       return res.json(files);

});

I was able to test this successfully.

这篇关于用Sails.js 0.10和Skipper使用Dropzone.js上传多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆