In the previous post, we learned:
- How to upload a files using DropZoneJs in ASP.NET MVC File upload in ASP.NET MVC using Dropzone JS and HTML5
- Limit Number of files upload using Dropzonejs Options – Part 1
- Removing thumbnails from dropzone js– Part 2
In this article, we will learn how to display existing files on server in dropzone js using ASP.NET MVC.
Implementation
Step 1
Create Attachment Model class under models folder. This class will hold the list of images from the server.
public class AttachmentsModel
{
public long AttachmentID { get; set; }
public string FileName { get; set; }
public string Path { get; set; }
}
Step 2
Create an Action Method that will return the images from the server.
public ActionResult GetAttachments()
{
var attachmentsList = new List<AttachmentsModel>
{
new AttachmentsModel {AttachmentID = 1,
FileName = "/images/wallimages/dropzonelayout.png",
Path = "/images/wallimages/dropzonelayout.png"},
new AttachmentsModel {AttachmentID = 1,
FileName = "/images/wallimages/imageslider-3.png",
Path = "/images/wallimages/imageslider-3.png"}
}.ToList();
return Json(new { Data = attachmentsList }, JsonRequestBehavior.AllowGet);
}
Step 3
Call the action method and display the images into the dropzone form. Here, we can use the Dropzone Init() function
to load the images:
<script type="text/javascript">
Dropzone.options.dropzoneForm = {
acceptedFiles: "image/*",
init: function () {
var thisDropzone = this;
$.getJSON("/home/GetAttachments/").done(function (data) {
if (data.Data != '') {
$.each(data.Data, function (index, item) {
var mockFile = {
name: item.AttachmentID,
size: 12345
};
thisDropzone.emit("addedfile", mockFile);
thisDropzone.emit("thumbnail", mockFile, item.Path);
});
}
});
}
};
</script>
The following is the JSON Result returned inside InIt()
function:
{"Data":[{"AttachmentID":1,
"FileName":"/images/wallimages/dropzonelayout.png",
"Path":"/images/wallimages/dropzonelayout.png"},
{"AttachmentID":1,
"FileName":"/images/wallimages/imageslider-3.png",
"Path":"/images/wallimages/imageslider-3.png"}]}
Output
Display existing files on server dropzone js in ASP.NET MVC jquery
Source Code
You can download the source from Github.
The post How to display existing files on server in dropzone js using ASP.NET MVC appeared first on Venkat Baggu Blog.