Recently, I encountered a project where I needed to achieve the same goal. After exploring various plugins, I came across the dmUploader
plugin, which proved to be extremely useful. This plugin allows you to upload images through drag and drop or using the input
tag. You can find the plugin HERE.
I have implemented a functional example on CODEPEN, which I believe is the most effective way to learn how to utilize this plugin. The jQuery code from lines 1 to 290 in the example mainly consists of the plugin itself. Since the plugin does not have a CDN link, I had to include it in the code. Let's review the code snippets:
HTML
<div class="SciSecPic">
<i class="fa fa-fw fa-picture-o"></i>
<label>
<span>Click to browse your picture</span>
<input type="file" title='Click to add Files' style="display:none;">
</label>
<div class="progress" style=" display:none;">
<div class="progress-bar progress-bar-striped active" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 0%;">
0%
</div>
</div>
</div>
In this example, you need a container div named SciSecPic
, a label
containing span
and hidden input
tags. The input
tag is hidden, and the span
tag serves as the button. Additionally, you can include a progress bar (Bootstrap progress bar in this case) within a div with the class progress
, which is only displayed while uploading.
No specific CSS settings are required for this, so I will skip that part and direct you to the functional example for reference.
jQuery - Implementing dmUploader
var readPic;
$(".SciSecPic").each(function () {
var self = $(this);
self.dmUploader({
//url: "/Something/ImageSaver",
url: "http://posttestserver.com/post.php?dir=ali",
dataType: "json",
allowedTypes: "image/*",
//extraData: {
//Name: self.data("name"),
//Id: ~~self.data("id")
//},
onInit: function () {
},
onNewFile: function (id, file) {
// showing progressbar
$(this).find(".progress").show(200);
/* preparing image for preview */
if (typeof FileReader !== "undefined") {
var reader = new FileReader();
reader.onload = function (e) {
readPic = e.target.result;
}
reader.readAsDataURL(file);
};
},
onUploadProgress: function (id, percent) {
$(this).find(".progress-bar").width(percent + "%").attr("aria-valuenow", percent).text(percent + "%");
},
onComplete: function () {
var thisEl = $(this);
addPic(thisEl, readPic);
// to fadeout and reset the progress bar at the end
setTimeout(function () {
thisEl.find(".progress").hide(200, function () {
thisEl.find(".progress-bar").width("0%").attr("aria-valuenow", 0).text("0%");
})
}, 300);
}
});
});
The variable readPic
stores the dataURL of the image to be displayed after uploading completes. You need to update the url
to point to the PHP file or controller if you are using MVC. Extra data can be included using the extraData
method. Various callback functions are available with dmUploader
, such as onUploadProgress
for displaying upload progress, onNewFile
for preparing the image for preview, and onComplete
for post-upload actions. In my case, I utilized the addPic
function to display the uploaded picture above the input box.
To keep this response concise, I won't delve into the addPic
function here. It is straightforward to comprehend. That covers the basics of using dmUploader effectively.