Drag and Drop Files and Images to ASP.NET MVC App - CodeProject


: 3


You can see the final result at http://youtu.be/7w0_D0qYVPo.

This is a tutorial about uploading files using drag and drop with ASP.NET MVC by adding the minimum amount of coding.


Drag and drop is a great HTML5 feature, we can leverage it to upload files to the server. This demo simplifies the whole process. However, if you need to customize it, it is important to understand drag and drop in HTML5, and you can add more features to this tool using this link.

Using the Code

In this tip, we will use the library from http://www.dropzonejs.com/ which provides us with basic functionality for dragging and dropping images and other file types directly to the browser.

First, use these files, dropzone.css and dropzone.js and locate them in this location:


Add them to bundle in the BundleConfig.cs which is usually located in the App_Start folder.

bundles.Add(new ScriptBundle("~/bundles/dropzonescripts").Include(

            bundles.Add(new StyleBundle("~/Content/dropzonescss").Include(

Now go to the Layout and add a style render call in the head like this @Styles.Render("~/Content/dropzonescss")
and in the end of the body tag, add this @Scripts.Render("~/bundles/dropzonescripts") so the simplest layout would be like this:

<!DOCTYPE html>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />

    @RenderSection("scripts", required: false)

In any controller (should be one controller in the project), we should add the upload action which accepts the uploaded files and saves them to the hard disk and then returns the file identification of the uploaded file (note that in this step, we can save the file to database or to another service). Currently, we will save the file to the folder (~/Images/uploaded) and if does not exist, it will be created. The disadvantage of this approach is (after publishing) that the user of the IIS which is normally IUSER should have a full permission on that folder.

public ActionResult SaveUploadedFile()
            bool isSavedSuccessfully = true;
            string fName = "";
                foreach (string fileName in Request.Files)
                    HttpPostedFileBase file = Request.Files[fileName];
                    //Save file content goes here
                    fName = Guid.NewGuid().ToString(); //file.FileName;
                    if (file != null && file.ContentLength > 0)
                        var originalDirectory = 
                                 new System.IO.DirectoryInfo(
                                      string.Format("{0}Images\\uploaded", Server.MapPath(@"\")));
                        string pathString = System.IO.Path.Combine(originalDirectory.ToString(),
                        var fileName1 = System.IO.Path.GetFileName(file.FileName);
                        bool isExists = System.IO.Directory.Exists(pathString);
                        if (!isExists)
                        var path = string.Format("{0}\\{1}", pathString, fName);
            catch (Exception ex)
                isSavedSuccessfully = false;

            if (isSavedSuccessfully == false )
                return Json(new { Message = fName });
                return Json(new { Message = "Error in saving file" });

You should add the File Upload Helper class to your project, it accepts a parameter which refers to the URL of the action we have just created:

namespace System.Web.Mvc
    public static class FileUploadFormHelper
        public static MvcHtmlString FileUploadForm(this HtmlHelper htmlHelper, string ControllerAction)
            string formTemplate = 
                   @"<form action='{0}' method='post' 
                      enctype='multipart/form-data' class='dropzone'  id='dropzoneForm'>
                                        <div class='fallback'>
                                            <input name='file' type='file' multiple />
                                            <input type='submit' value='Upload' />
            return new MvcHtmlString(string.Format(formTemplate, ControllerAction)); ;

Then, in the view that you want to show the uploader, you can use the helper method (Html.FileUploadForm) and pass to it the location of upload action as a parameter (note that the console.log raises an exception in Internet Explorer, so you can alert the message instead).


@section scripts{
<script type="text/javascript">
    Dropzone.options.dropzoneForm = {
        init: function () {
            myDropzone = this;
            this.on("complete", function (data) {
                var res = JSON.parse(data.xhr.responseText);