File upload 使用Sails.js 0.10和Skipper使用Dropzone.js上载多个文件

File upload 使用Sails.js 0.10和Skipper使用Dropzone.js上载多个文件,file-upload,sails.js,dropzone.js,skipper,File Upload,Sails.js,Dropzone.js,Skipper,我的sails应用程序中存在多个文件上载问题。 我试图用Dropzone.js实现多文件上传,我的后端是sailsv0.10.0-rc8 现在,当我通过dropzone上传一些文件时,我看到在多次上传的情况下,它会在请求中发送带有单独参数的文件。参数名是'photo[0]、'photo[1]、'photo[2]、…。我在控制器中获取的文件如下: req.file(file).upload(function (err, files) { // save the file }); 但当

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

现在,当我通过dropzone上传一些文件时,我看到在多次上传的情况下,它会在请求中发送带有单独参数的文件。参数名是
'photo[0]、'photo[1]、'photo[2]、…
。我在控制器中获取的文件如下:

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

    // save the file

});
但当提交了不止一个文件时,在解析和存储请求中的所有文件之前,请求会被传递到控制器,所以我的控制器中只有一个文件


有没有人经历过这个问题?可能在skipper body解析器中不支持使用不同请求参数的多个文件上载?因为当我在一个属性('photo')中提交多个文件时,所有文件都会被处理并传递给控制器。

如果您异步循环参数名称,这应该可以工作,例如:

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

});

我成功地测试了它。

这对我来说似乎很好:

    Dropzone.options.fotagDropzone = {
    init: function() {

    this.on("success", function(file, responseText) {
    // Handle the responseText here. For example, add the text to the preview element:
    console.log(responseText.files[0]);
    file.previewTemplate.appendChild(document.createTextNode(responseText.files[0].fd));
    });

    },
    paramName: "file", // The name that will be used to transfer the file
    maxFilesize: 10, // MB
    uploadMultiple: false,
    addRemoveLinks: true,
    parallelUploads: true,
    dictDefaultMessage: 'Drag files here <br />or<br /><button class="dzUploadBtn" type="button">click here to upload</button>',
    acceptedMimeTypes: '.jpg'
    };
Dropzone.options.fotagDropzone={
init:function(){
this.on(“success”,函数(文件,responseText){
//在此处处理responseText。例如,将文本添加到预览元素:
console.log(responseText.files[0]);
appendChild(document.createTextNode(responseText.files[0].fd));
});
},
paramName:“file”,//将用于传输文件的名称
最大文件大小:10,//MB
uploadMultiple:false,
addRemoveLinks:是的,
并行上传:对,
dictDefaultMessage:“将文件拖到此处
或单击此处上载”, AcceptedMitypes:“.jpg” };

基本上,它不会同时发送所有文件,但您仍然可以将多个文件放入dropzone。后端是使用skipper的标准上载。

使用Dropzone和Sails.js,您必须:

  • 在dropzone配置中添加文件名的定义:
Dropzone.options.myDropzone={ 参数名称:“文件名” }

  • 使用此命令返回上载的文件:
请求文件('fileName').upload(函数(err,uploadedFiles){

}))


uploadedFiles包含文件

此代码是否包含在某种循环中?
req.file
的参数是发送文件的参数,在您的情况下,所有文件都是使用不同的参数发送的,因此,
.upload
会导致只返回一个文件。实际上,我不知道对
req.file
的多次调用是否有效,但首先让我们明确一下您是否在尝试。是的,我有一个循环,其中包含所有可能的文件名,嗯。。。每次保存文件后,我都会使用
async.eachSeries
,就像我做一些其他事情一样,这对我很有用。感谢您提醒我有关async;)但是如何从req获取文件参数呢?(在上述ex.ParamName中)完美!,有益的