【问题标题】:Uploading multiple files with Sails.js 0.10 and Skipper using Dropzone.js使用 Sails.js 0.10 和 Skipper 使用 Dropzone.js 上传多个文件
【发布时间】:2016-11-29 17:02:53
【问题描述】:

我的 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

});

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

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

【问题讨论】:

  • 这段代码是否包含在某种循环中? req.file 的参数是发送文件的参数,在您的情况下,所有文件都是使用不同的参数发送的,因此 .upload 会导致只返回一个文件。我实际上不知道多次调用req.file 是否会起作用,但首先让我们弄清楚你是否正在尝试。
  • 是的,我有一个包含所有可能文件名的循环

标签: file-upload sails.js dropzone.js skipper


【解决方案1】:

这应该可以工作,前提是您异步遍历参数名称,例如:

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

});

我成功地测试了这个。

【讨论】:

  • 嗯...我最终使用了async.eachSeries,因为我在每次保存文件后都会做一些其他的事情,它对我有用。感谢您提醒异步;)
  • 但是如何从 req. ? (在上面的例子中。paramNames)
  • 完美!,有帮助
【解决方案2】:

这对我来说似乎没问题:

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

【讨论】:

    【解决方案3】:

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

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

    Dropzone.options.myDropzone = { 参数名称:“文件名” }

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

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

    });

    uploadedFiles 包含文件

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-10
      • 2015-09-13
      • 2019-10-01
      • 1970-01-01
      • 2016-12-23
      • 2014-11-21
      • 1970-01-01
      相关资源
      最近更新 更多