Enable copy and paste files in dropzone.js

后端 未结 3 1230
庸人自扰
庸人自扰 2020-12-14 21:13

I am using dropzone.js. I want to implement the \"Copy & Paste\" feature in it.

What I tried is:

Inside dropzone.js:<

相关标签:
3条回答
  • 2020-12-14 22:07

    If you don't want to use other JS libraries, you can integrate dropzone with a paste event fairly easily by retrieving the data as a file from the paste event:

    // create dropzone however you wish
    var myDropzone = new Dropzone("div#element", { url: "/path/to/upload"});
    // add paste event listener to the page
    document.onpaste = function(event){
      var items = (event.clipboardData || event.originalEvent.clipboardData).items;
      for (index in items) {
        var item = items[index];
        if (item.kind === 'file') {
          // adds the file to your dropzone instance
          myDropzone.addFile(item.getAsFile())
        }
      }
    }
    
    0 讨论(0)
  • 2020-12-14 22:10
    var myDropzone = new Dropzone(".dropzone", { });
    
    document.onpaste = function(event){
      var items = (event.clipboardData || event.originalEvent.clipboardData).items;
      for (index in items) {
        var item = items[index];
        if (item.kind === 'file') {
          // adds the file to your dropzone instance
          myDropzone.addFile(item.getAsFile())
        }
      }
    }
    

    Just add this code. Do not declare URL because URL also declared in PHP or coding file, paste this code in view file (HTML, PHP, etc).

    0 讨论(0)
  • 2020-12-14 22:12

    This worked for me. It uses the FileReaderJS wrapper. As I am not creating the dropzone programatically, I had to store it at runtime with the init() method.

    See here for the FileReaderJS part.

    var myDropzone;
    
    function checkUploadFile(filename) {
        //do some input checking here, if you want
        return true;
    }
    
    Dropzone.options.FileDropUploadZone = {
      paramName: "myDiv",
      maxFilesize: 3, // MB
      uploadMultiple: true,
      addRemoveLinks: true,
      acceptedFiles: 'image/*',
      maxFiles: 10,
      accept: function(file, done) {
          if (!checkUploadFile(file.name)) {
    
                    done('Invalid file');
                    myDropzone.removeFile(file);
    
          }
          else { done(); }
      },
      init: function() {
          myDropzone = this;
      }
    };
    
    $(document).ready(function () {
            FileReaderJS.setupClipboard(document.body, {
                accept: {
                    'image/*': 'DataURL'
                },
                on: {
                    load: function(e, file) {
                    myDropzone.addFile(file);
                    }
                }
            });
    });
    
    0 讨论(0)
提交回复
热议问题