Dropzone.js - Display existing files on server

前端 未结 5 1877
我寻月下人不归
我寻月下人不归 2020-12-15 10:04

I\'m currently using dropzone.js v3.10.2 I am having issues displaying my existing files I have already uploaded. I am more than competent with php however I have limited kn

相关标签:
5条回答
  • 2020-12-15 10:25

    This is the way which I implemented it. I have used createThumbnailFromUrl rather using emitting a thumbnail event.

    HTML element;

    <form id="sample-img" action="/upload" class="dropzone">
        <div class="dz-default dz-message"></div>
    </form>
    

    JS code;

    previewThumbailFromUrl({
        selector: 'sample-img',
        fileName: 'sampleImg',
        imageURL: '/images/sample.png'
    });
    
    function previewThumbailFromUrl(opts) {
        var imgDropzone = Dropzone.forElement("#" + opts.selector);
        var mockFile = {
            name: opts.fileName,
            size: 12345,
            accepted: true,
            kind: 'image'
        };
        imgDropzone.emit("addedfile", mockFile);
        imgDropzone.files.push(mockFile);
        imgDropzone.createThumbnailFromUrl(mockFile, opts.imageURL, function() {
            imgDropzone.emit("complete", mockFile);
        });
    }
    
    0 讨论(0)
  • 2020-12-15 10:27

    For Dropzone 5.x

    The solutions given so far did not work with dropzone version 5.x. What worked for me was to modify dropzone's config options as follows:

    init: function () {
                    var mockFile = {
                        name: 'FileName',
                        size: '1000', 
                        type: 'image/jpeg',
                        accepted: true            // required if using 'MaxFiles' option
                    };
                    this.files.push(mockFile);    // add to files array
                    this.emit("addedfile", mockFile);
                    this.emit("thumbnail", mockFile, 'http://url/to/file');
                    this.emit("complete", mockFile); 
                }
    

    The concept is, to create a mock file, and call the event handlers addedfile and thumbnail to draw the preview. And then finally call on complete event to ensure that there are no progress bars, etc. being displayed.

    Reference

    0 讨论(0)
  • 2020-12-15 10:41

    I'm leaving here the solution that worked for me. (Dropzone v5.7.0 and Codeigniter v3.1.11)

    1. Use Dropzone option init
    2. Make AJAX call to back-end script which returns json encoded array of objects containing images' info (PHP way above in the question, Codeigniter way here)
    3. Use jQuery each function to iterate on every object containing image info
    4. Use the snippet here: https://github.com/enyo/dropzone/wiki/FAQ#how-to-show-files-already-stored-on-server to display each image

    in my App controller:

    public function ilan_fotolari($ilan_id = 0)
    {
        $this->load->helper('ilan');
        $dirPath = './assets/uploads/'.ilan_dir($ilan_id);
        $this->load->helper('file');
        $file_names = get_filenames($dirPath);
        $mocks = [];
        foreach ($file_names as $file_name) {
            $mock['name'] = $file_name;
            $dirUrl = base_url('assets/uploads/'.ilan_dir($ilan_id));
            $mock['url'] = $dirUrl.$file_name;
            $mocks[] = $mock;
        }
        $this->output
            ->set_content_type('application/json')
            ->set_output(json_encode($mocks));
    }
    

    in my script.js:

    Dropzone.options.ilanFotoAlani = {
        paramName: "foto", // The name that will be used to transfer the file
        maxFilesize: 5, // MB
        maxFiles: 9,
        resizeWidth: 1000,
        resizeHeight: 644,
        resizeMimeType: 'image/jpeg',
        addRemoveLinks: true,
        dictDefaultMessage: 'Fotoğraf sürükle veya seç',
        dictFileTooBig: 'Fotoğraf boyutu en fazla 5MB olmalı',
        dictRemoveFile: 'Fotoğrafı sil',
        dictCancelUpload: 'İptal et',
        dictMaxFilesExceeded: 'En fazla 9 fotoğraf yükleyebilirsin',
        init: function () {
            let myDropzone = this;
            $.ajax({
                type: 'get',
                url: '/ilan-fotolari',
                success: function(mocks){
                    $.each(mocks, function(key,value) {
                        let mockFile = { name: value.name, size: 1024 };
                        myDropzone.displayExistingFile(mockFile, value.url);
                    });
                },
                error: function(xhr, durum, hata) {
                    alert("Hata: " + hata);
                }
            });
        }
    };
    

    I have mixed up solutions from different solutions into this.

    0 讨论(0)
  • 2020-12-15 10:44

    I was having trouble with maxFiles/maxfilesexceeded and take some while looking for a anwser and then I found this links below:

    https://www.bountysource.com/issues/1444854-use-files-already-stored-on-server-wiki-example-incomplete?utm_campaign=plugin&utm_content=tracker%2F283989&utm_medium=issues&utm_source=github

    $.each(obj, function(i, item) {
    
      ///faking the BytesSent its not essanail to me.. if you need it just pass the correct one
      var upload = {bytesSent: 12345};
    
      /// again fake the size..
      var mockFile = {name: item.name, size: 12345, accepted: true};
    
      mockFile.upload = upload;
      mockFile.kind = "file";
      Dropzone.forElement("#editfileuploader").emit("addedfile", mockFile);
    
      //push the file to files array because getAcceptedFiles using this array length to get the currct files count..
      Dropzone.forElement("#editfileuploader").files.push(mockFile);
    
      //this for lettig dropzone to creat the thumbnail(item.ts has the file location)
      Dropzone.forElement("#editfileuploader").emit("thumbnail", mockFile, item.ts);
    
      //to show the success mark and to return image id response
      Dropzone.forElement("#editfileuploader").emit("success", mockFile, item.id);
    }); 
    
    0 讨论(0)
  • 2020-12-15 10:49

    I checked the code (from starTutorial) and it didn't work for me either(?)

    I managed to get it working by replacing this:

    $.get('upload.php', function(data) {
      $.each(data, function(key,value) {
        var mockFile = { name: value.name, size: value.size };
        thisDropzone.options.addedfile.call(thisDropzone, mockFile);
        thisDropzone.options.thumbnail.call(thisDropzone, mockFile, "uploads/"+value.name);
      });
    });
    

    with this:

    $.getJSON('files/list-all', function(data) {
      $.each(data, function(index, val) {
        var mockFile = { name: val.name, size: val.size };
        thisDropzone.options.addedfile.call(thisDropzone, mockFile);
        thisDropzone.options.thumbnail.call(thisDropzone, mockFile, "uploads/" + val.name);
      });
    });
    

    Credit to this answer: https://stackoverflow.com/a/5531883/984975

    EDIT: In version 4.0 the thumbnails of the existing files will be showed with the cue bar in it. To solve this add:

    thisDropzone.emit("complete", mockFile);
    
    0 讨论(0)
提交回复
热议问题