首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Dropzone.js中构建一个文件名列表以传递表单提交

在Dropzone.js中构建一个文件名列表以传递表单提交
EN

Stack Overflow用户
提问于 2017-11-17 12:42:53
回答 1查看 2.2K关注 0票数 0

我一直在努力构建一个文件名列表,这些文件名在Dropzone.js中排队等待上传。几个星期以来,我一直在论坛上寻找答案。https://github.com/enyo/dropzone/issues/1652

我从这里开始:https://github.com/enyo/dropzone/wiki/Upload-all-files-with-a-button

在代码的这一节中,我正在工作:

代码语言:javascript
复制
    this.on("addedfile", function() {
      console.log(this.getAcceptedFiles(file));
    });

此函数返回关于要在单击时上载的排队文件的信息数组。数组如下所示:

代码语言:javascript
复制
[File(75099)]
   0: File(75099)
     accepted: true
     lastModified: 1508061061300
     lastModifiedDate: Sun Oct 15 2017 05:51:01 GMT-0400 (Eastern Daylight  Time) {}
     name: "ITI-TV-REPAIR-FORM.pdf"
     previewElement: div.dz-preview.dz-file-preview
     previewTemplate: div.dz-preview.dz-file-preview
     size: 75099
     status: "queued"
     type: "application/pdf"
     upload: {progress: 0, total: 75099, bytesSent: 0}
     webkitRelativePath: ""
     _removeLink: a.dz-remove
     __proto__: File
     length: 1
     __proto__: Array(0)

我知道如果我使用this.getAcceptedFiles().length。由于长度位于数组的“根”中,因此它将返回1。但想要查到这个名字却让我很困惑。最难的部分是数组顶部的文件(75099)总是会改变,但是键名总是在同一个位置。

我在打字的时候想得很大声。我刚刚想到的是,如果我能够对数组进行键值搜索,因为我知道我要寻找的键是name:。

我尝试访问名称值

代码语言:javascript
复制
this.getAcceptedFiles().name

当然,这是行不通的。因此,我的最后一个问题是,访问这个数组中的键值以获取文件名的最佳方法是什么?

更新:想清楚!

代码语言:javascript
复制
            this.on("addedfile", function(file) {
                //returns file names that are in the queue
                console.log(file.name);

            });

完整代码在这里:https://github.com/enyo/dropzone/issues/1652

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-17 13:35:15

每当将文件添加到上载文件列表中时,都会调用addedfile事件,还可以在事件回调中作为参数获取添加的文件:

代码语言:javascript
复制
this.on("addedfile", function(file) {
    console.log(file.name)
});

下面是一个有用的例子:

代码语言:javascript
复制
Dropzone.options.myDropzone = {

  // Prevents Dropzone from uploading dropped files immediately
  autoProcessQueue: false,
  
  acceptedFiles: 'image/*',

  init: function() {
    var submitButton = document.querySelector("#submit-all");
    myDropzone = this;

    submitButton.addEventListener("click", function() {
      myDropzone.processQueue(); // Tell Dropzone to process all queued files.
    });

    // You might want to show the submit button only when 
    // files are dropped here:
    this.on("addedfile", function(file) {
      console.log(file.name)
    });

  }
};
代码语言:javascript
复制
<!DOCTYPE html>

<script src="https://rawgit.com/enyo/dropzone/master/dist/dropzone.js"></script>
<link rel="stylesheet" href="https://rawgit.com/enyo/dropzone/master/dist/dropzone.css">

<!-- Change /upload-target to your upload address -->
<form action="/upload-target" class="dropzone" id="my-dropzone"></form>
<br>
<button id="submit-all">Submit all files</button>

希望这能有所帮助

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47350923

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档