6

我的 Sails 应用程序中存在多个文件上传问题。我正在尝试使用 Dropzone.js 实现多个文件上传,我的后端是 Sails v0.10.0-rc8。

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

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

    // save the file

});

但是,当提交的文件不止一个时,请求会在从请求中解析和存储所有文件之前传递给控制器​​,所以我的控制器中只有一个文件。

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

4

3 回答 3

8

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

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

});

我能够成功地对此进行测试。

于 2014-07-26T21:45:40.833 回答
1

这对我来说似乎没问题:

    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 中。后端是您使用船长的标准上传。

于 2015-05-30T12:43:59.783 回答
0

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

  • 在 dropzone 配置中添加文件名的定义:

Dropzone.options.myDropzone = { paramName: "fileName" }

  • 使用此命令取回上传的文件:

req.file('fileName').upload(function (err, uploadFiles) {

});

UploadFiles 包含文件

于 2016-11-29T09:02:53.537 回答