【问题标题】:Adding form fields to each file in Dropzone.js向 Dropzone.js 中的每个文件添加表单字段
【发布时间】:2015-06-16 17:58:43
【问题描述】:

我已经尝试让它工作了将近一个星期。最后我会寻求帮助。我确定我错过了一些简单的东西。

我正在使用 DropZone.js 允许将图片上传到网络服务器。我需要为每次上传添加一个文本框,以便用户可以在图像中输入其他信息或“标签”,以便以后可以通过这些标签过滤它们。我可以得到一个输入来显示每个添加的图像,但是当我试图在“发送”事件期间读取这些文本框中的值时,我得到的只是第一个文本框的值。因此,如果他们上传 5 张图片,所有图片都将通过在第一个文本框中输入的“标签”。

感谢您提供的任何帮助。

<div class="table table-striped" class="files" id="previews">
    <div id="template" class="file-row">
        <!-- This is used as the file preview template -->
        <div>
            <span class="preview">
                <img data-dz-thumbnail /></span>
        </div>
        <div>
            <p class="name" data-dz-name></p>
            <strong class="error text-danger" data-dz-errormessage></strong>
        </div>
        <div>
            <p class="tags" data-dz-tags></p>
            <input type="text" id="tags" name="tags" />
        </div>
        <div>
            <p class="size" data-dz-size></p>
            <div class="progress progress-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="0">
                <div class="progress-bar progress-bar-success" style="width: 0%;" data-dz-uploadprogress></div>
            </div>
        </div>
        <div>
            <button class="btn btn-primary start">
                <i class="glyphicon glyphicon-upload"></i>
                <span>Start</span>
            </button>
            <button data-dz-remove class="btn btn-warning cancel">
                <i class="glyphicon glyphicon-ban-circle"></i>
                <span>Cancel</span>
            </button>
            <button data-dz-remove class="btn btn-danger delete">
                <i class="glyphicon glyphicon-trash"></i>
                <span>Delete</span>
            </button>
        </div>
    </div>

</div>

还有 javascript:

    <script>
    // Get the template HTML and remove it from the doument
    var previewNode = document.querySelector("#template");
    previewNode.id = "";
    var previewTemplate = previewNode.parentNode.innerHTML;
    previewNode.parentNode.removeChild(previewNode);

    var myDropzone = new Dropzone(document.body, { // Make the whole body a dropzone
        url: "project.aspx", // Set the url
        thumbnailWidth: 80,
        thumbnailHeight: 80,
        parallelUploads: 20,
        previewTemplate: previewTemplate,
        autoQueue: false, // Make sure the files aren't queued until manually added
        previewsContainer: "#previews", // Define the container to display the previews
        clickable: ".fileinput-button" // Define the element that should be used as click trigger to select files.
    });

    myDropzone.on("addedfile", function (file) {
        // Hookup the start button
        file.previewElement.querySelector(".start").onclick = function () { myDropzone.enqueueFile(file); };
    });

    // Update the total progress bar
    myDropzone.on("totaluploadprogress", function (progress) {
        document.querySelector("#total-progress .progress-bar").style.width = progress + "%";
    });

    myDropzone.on("sending", function (file, xhr, formData) {
        formData.append("tags", document.getElementById('tags').value);
        // Show the total progress bar when upload starts
        document.querySelector("#total-progress").style.opacity = "1";
        // And disable the start button
        file.previewElement.querySelector(".start").setAttribute("disabled", "disabled");
    });

    // Hide the total progress bar when nothing's uploading anymore
    myDropzone.on("queuecomplete", function (progress) {
        document.querySelector("#total-progress").style.opacity = "0";
    });

    // Setup the buttons for all transfers
    // The "add files" button doesn't need to be setup because the config
    // `clickable` has already been specified.
    document.querySelector("#actions .start").onclick = function () {
        myDropzone.enqueueFiles(myDropzone.getFilesWithStatus(Dropzone.ADDED));
    };
    document.querySelector("#actions .cancel").onclick = function () {
        myDropzone.removeAllFiles(true);
    };
</script>

这都包含在一个 aspx 页面中并回发给它自己。在后面的代码中,我试图读取表单的值。但我只能取回放在第一个文本框中的值。

谢谢, 基思

【问题讨论】:

  • 这个解决方案有什么运气吗?我正在尝试使用 react-dropzone-component 来实现它
  • 解决方案好运吗?

标签: javascript jquery html asp.net dropzone.js


【解决方案1】:

你会用这个得到你的数据

var tag= file.previewElement.querySelector('input[name="tags"]').value
formData.append("tags", tag);

这对我有用,但我的输入没有 id

<p class="tags" data-dz-tags></p>
<input type="text" name="tags" />

【讨论】:

  • 感谢@Tjeans,它对我有用,不明白为什么 dropzone 没有解释...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-18
  • 1970-01-01
  • 2011-12-04
相关资源
最近更新 更多