How can I show you the files already stored on server using Dropzone.js
Solution 1:
Finally !!
$(function() {
var mockFile = { name: "banner2.jpg", size: 12345 };
var myDropzone = new Dropzone("#my-awesome-dropzone");
myDropzone.options.addedfile.call(myDropzone, mockFile);
myDropzone.options.thumbnail.call(myDropzone, mockFile, "http://localhost/test/drop/uploads/banner2.jpg");
})
Solution 2:
You can also do with the following code:
<script>
Dropzone.options.myAwesomeDropzone = false;
Dropzone.autoDiscover = false;
$("#image").dropzone({
url: "http://someserver.com/upload.php",
paramName: "image", // The name that will be used to transfer the file
maxFilesize: 2, // MB
maxFiles: 5,
parallelUploads: 5,
addRemoveLinks: true,
dictMaxFilesExceeded: "You can only upload upto 5 images",
dictRemoveFile: "Delete",
dictCancelUploadConfirmation: "Are you sure to cancel upload?",
accept: function (file, done) {
console.log(file)
if ((file.type).toLowerCase() != "image/jpg" &&
(file.type).toLowerCase() != "image/gif" &&
(file.type).toLowerCase() != "image/jpeg" &&
(file.type).toLowerCase() != "image/png"
) {
done("Invalid file");
}
else {
done();
}
},
init: function () {
var mockFile = { name: "myimage.jpg", size: 12345, type: 'image/jpeg' };
this.addFile.call(this, mockFile);
this.options.thumbnail.call(this, mockFile, "http://someserver.com/myimage.jpg");
}
});
</script>
EDIT
Since update of Dropzone 4.0 init
function can be called as:
init: function () {
var mockFile = { name: "myimage.jpg", size: 12345, type: 'image/jpeg' };
this.options.addedfile.call(this, mockFile);
this.options.thumbnail.call(this, mockFile, "http://someserver.com/myimage.jpg");
mockFile.previewElement.classList.add('dz-success');
mockFile.previewElement.classList.add('dz-complete');
}
Solution 3:
my solution for >= 4.0, basing on "How to show files already stored on server": https://github.com/enyo/dropzone/wiki/FAQ
maxFiles: 1,
init: function () {
this.on('maxfilesexceeded', function (file) {
this.removeAllFiles();
this.addFile(file);
});
var mocks = $dropzone.data('dropzone');
for (var i = 0; i < mocks.length; i++) {
var mock = mocks[i];
mock.accepted = true;
this.files.push(mock);
this.emit('addedfile', mock);
this.createThumbnailFromUrl(mock, mock.url);
this.emit('complete', mock);
}
}
Solution 4:
Based on punky's excellent answer above, you should not forget to add this._updateMaxFilesReachedClass();
at the end, like so:
init: function () {
var mockFile = { name: <filename>, size: <filesize>, type: <filetype>, url: <file_url> };
this.files.push(mockFile);
this.emit('addedfile', mockFile);
this.createThumbnailFromUrl(mockFile, mockFile.url);
this.emit('complete', mockFile);
this._updateMaxFilesReachedClass();
}
Solution 5:
In this answer https://stackoverflow.com/a/17763511, it's implemeneted with emmiting a thumbnail event.
Following is an example of doing it using createThumbnailFromUrl.
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);
});
}
Working Samples on JSFiddle:
- Load images on same domain
- Load images with crossOrigin