简体   繁体   English

如何在jQuery中使拖放区可点击?

[英]How to make drop-zone clickable in jQuery?

In my web form application, I have used a drop-zone where users will drop files to upload them. 在我的Web表单应用程序中,我使用了放置区,用户可以在其中放置文件以上传文件。 At the movement, when I drop a file on the drop-zone, it works fine and I can easily upload that file by clicking the button located down the drop-zone. 在移动时,当我将文件拖放到拖放区时,它可以正常工作,并且我可以通过单击位于拖放区下方的按钮轻松上传该文件。

However, I want to open the upload file dialog when someone will click on the drop-zone. 但是,当有人单击放置区域时,我想打开上载文件对话框。 How can I make the drop-zone clickable so that upload file dialog is shown where I can choose the file to upload. 如何使拖放区可单击,以便显示上传文件对话框,从中可以选择要上传的文件。 I have searched different techniques but nothing works fine. 我搜索了不同的技术,但没有任何效果。 Is there any way that can help me achieve my goal easily? 有什么方法可以帮助我轻松实现目标?

My drop-zone HTML is here. 我的拖放区HTML在这里。

<div id="dZUpload" class="dropzone">
 <div class="dz-default dz-message"></div>
</div>

I am looking to achieve this goal with the following jQuery code in document.ready. 我希望通过document.ready中的以下jQuery代码实现此目标。

              var userEmail = $("#hdnFolderPath").val();
              var uploadButton = document.querySelector("#upload");

              Dropzone.autoDiscover = false;

              $("#dZUpload").dropzone({
                  url: "/ReceiptStorage/Handlers/FileHandler.ashx",
                  params: {
                      DestinationPath: userEmail
                  },
                  autoProcessQueue: false,
                  addRemoveLinks: true,

                  init: function () {
                      var uploadButton = document.querySelector("#upload");
                      var dZUpload = this; //closure

                      dZUpload.on("complete", function (file, response) {
                          if (file.status === 'success') {
                              dZUpload.removeFile(file);
                              LoadFiles($("#hdnFolderPath").val());
                          }
                      });
                      dZUpload.on('error', function (file, response) { 
                      });
                      uploadButton.addEventListener("click", function () {
                          if (dZUpload.files.length > 0)
                              dZUpload.processQueue();
                      });
                  }    
              });

This is the JQuery that is used as the template for every file dropped: 这是用作每个删除文件模板的JQuery

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: "/target-url", // 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) {
  // 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);
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM