How do I preload images into dropzone.js

后端 未结 3 1971
野的像风
野的像风 2020-12-23 11:29

I have a dropzone.js instance on a web page with the following options:

autoProcessQueue:false
uploadMultiple:true
parallelUploads:20
maxFiles:20
         


        
3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-23 12:25

    Dropzone is so strong and awesome that you can do anything on it .
    Steps to Follow -- >

    1)First of all you will have to create a server side script which will get all the filenames and its size and send it as json response.
    PHP code :

      foreach($myitemfiles as $file){ //get an array which has the names of all the files and loop through it 
            $obj['name'] = $file; //get the filename in array
            $obj['size'] = filesize("uploadsfolder/".$file); //get the flesize in array
            $result[] = $obj; // copy it to another array
          }
           header('Content-Type: application/json');
           echo json_encode($result); // now you have a json response which you can use in client side 
    

    2)Now you can use the response to display the uploaded files.Write the below code inside the dropzone init function
    Javascript Code :

      init: function() {
    
          var thisDropzone = this;
    
            $.getJSON('get_item_images.php', function(data) { // get the json response
    
                $.each(data, function(key,value){ //loop through it
    
                    var mockFile = { name: value.name, size: value.size }; // here we get the file name and size as response 
    
                    thisDropzone.options.addedfile.call(thisDropzone, mockFile);
    
                    thisDropzone.options.thumbnail.call(thisDropzone, mockFile, "uploadsfolder/"+value.name);//uploadsfolder is the folder where you have all those uploaded files
    
                });
    
            });
    }
    

    Note : don't take the whole file url path in filename just take the filename itself thats it .
    This works

提交回复
热议问题