The Wayback Machine - https://web.archive.org/web/20130116160736/http://www.codeguru.com/csharp/csharp/cs_internet/uploading-files-using-html5-drag-and-drop-and-asp.net.htm

Uploading Files Using HTML5 Drag-and-Drop and ASP.NET

Introduction

ASP.NET web applications that require uploading files from the client machine to the server use the file field to select files. The selected files are uploaded to the server using form submission techniques. In addition to the file field, HTML5 also allows you to select files using drag and drop. Using this feature you can drag files from Windows Explorer or Desktop and drop them on a predefined area of a web page. The files can then be uploaded to the server. This article illustrates how the HTML5 drag and drop feature can be used to upload files on the server.  

Selecting Local Files using HTML5 Drag and Drop

Traditionally you use the HTML file field to select files that are to be uploaded to the server. ASP.NET web forms wrap the file field into the FileUpload server control and ASP.NET MVC applications can use an <input> element with type attribute set to file. Another alternative offered by HTML5 is dragging one or more files from Windows Explorer or Desktop and drop them onto some predefined HTML element of a web page. You can then access the dropped files using the dataTransfer object available to drag and drop events. Discussing HTML5 drag and drop from the ground up is beyond the scope of this article. If you are unfamiliar with HTML5 drag and drop read this article first.

To understand how files can be selected using the drag and drop features of HTML5, let's develop a new ASP.NET web forms application. The HTML markup of the default web form is shown below:

<form id="form1" runat="server">
<center>
  <div id="box">Drag & Drop files from your machine on this box.</div>
  <br />
  <input id="upload" type="button" value="Upload Selected Files" />
</center>
</form>

As you can see, the <form> consists of a <div> element and a button. The <div> element is intended to drop the files dragged from the local machine. Merely dropping the files won't upload them to the server. Clicking on the button initiates the file upload operation.

To handle the file drop operation you need to wire certain event handlers to the box <div> element. The following jQuery code shows how that can be done:

var selectedFiles;

$(document).ready(function () {
  var box;
  box = document.getElementById("box");
  box.addEventListener("dragenter", OnDragEnter, false);
  box.addEventListener("dragover", OnDragOver, false);
  box.addEventListener("drop", OnDrop, false);
...
}

The code declares a global variable named selectedFiles for storing a list of selected files. The ready() function wires three event handler functions to the respective events of the box <div> element, viz. dragenter, dragover and drop using addEventListener() method. The first parameter of the addEventListener() method is the event name and the second parameter is the event handler function. The event handler functions are shown below:

function OnDragEnter(e) {
  e.stopPropagation();
  e.preventDefault();
}

function OnDragOver(e) {
  e.stopPropagation();
  e.preventDefault();
}

function OnDrop(e) {
  e.stopPropagation();
  e.preventDefault();
  selectedFiles = e.dataTransfer.files;
  $("#box").text(selectedFiles.length + " file(s) selected for uploading!");
}

The OnDragEnter() and OnDragOver() event handler functions are simple and they merely prevent the event bubbling of the respective events. The OnDrop() function is important since it handles the drop event. The list of files dragged and dropped on the <div> element is obtained using the files property of the dataTransfer object. The files object is of type FileList and each item of the FileList collection is of type File. These two objects are available as a part of the HTML5 File API. The OnDrop() function stores the selected files in the global variable - selectedFiles and displays a message in the <div> using the text() method that indicates the number of files selected. The following figure shows how the default web form looks after dragging and dropping files on the <div> element.

Default web form after dragging and dropping files to the <div> element
 

Default web form after dragging and dropping files to the <div> element

Sending Files to the Server Using jQuery

To send the selected files from the client to the server you can use different techniques but in this example you will use jQuery $.ajax() method for uploading the files. The following code shows how $.ajax() method can be used for this purpose.

$("#upload").click(function () {
  var data = new FormData();
  for (var i = 0; i < selectedFiles.length; i++) {
    data.append(selectedFiles[i].name, selectedFiles[i]);
  }
  $.ajax({
    type: "POST",
    url: "FileHandler.ashx",
    contentType: false,
    processData: false,
    data: data,
    success: function (result) {
      alert(result);
    },
    error: function () {
      alert("There was error uploading files!");
    }
  });
});

The code shown above first creates a FormData object. The FormData object encapsulates form's data that you wish to send to the server. All the selected files are added to the FormData object using its append() method. The first parameter of the append() method is the name of the file being added and the second parameter is the File object itself. Once the FormData object is ready you make a POST request to a Generic ASP.NET Handler (FileHandler.ashx) using jQuery $.ajax() method. You will create the generic handler in the next section.

The type of the request is POST. The url is FileHandler.ashx. Notice that the processData option is set to false. By default when you use the $.ajax() method the data is sent in URL encoded format. To prevent this behavior processData is set to false. The data option  is set to the FormData object created earlier. The success function simply displays the message returned by the generic handler. The error handler function displays an error message in case there is any error while calling FileHandler.ashx.

Receiving the Uploaded Files on the Server

The ASP.NET generic handler - FileHandler.ashx - receives the files sent by the $.ajax() method. The generic handler also saves them to a folder on the server. The following code shows how the handler accomplishes this task:

public void ProcessRequest(HttpContext context)
{
    if (context.Request.Files.Count > 0)
    {
        HttpFileCollection files = context.Request.Files;
        foreach (string key in files)
        {
            HttpPostedFile file = files[key];
            string fileName = file.FileName;
            fileName = context.Server.MapPath("~/uploads/" + fileName);
            file.SaveAs(fileName);
        }
    }
    context.Response.ContentType = "text/plain";
    context.Response.Write("File(s) uploaded successfully!");
}

The ProcessRequest() method of the FileHandler.ashx is called when the files are sent to the server using the $.ajax() method. The uploaded files can be accessed using the Files collection of the Request object. Each item inside the Files collection is of type HttpPostedFile. A foreach loop iterates through all the files from the Files collection and saves the individual file using the SaveAs() method of HttpPostedFile class. Once all files are saved a success message is sent to the client.

Note that by default ASP.NET sets request length to 4096 bytes. If you wish to upload large files you may adjust the request length using web.config file as shown below:

 <httpRuntime 
   maxRequestLength="20000"
   requestValidationMode="4.5" 
   targetFramework="4.5" 
   encoderType="..." />

As you can see the maxRequestLength attribute of the <httpRuntime> section is set to 20000 bytes. You need to adjust this value as per your requirement.

That's it! You can now run the web form, drag and drop files on the <div> element and click on the "Upload Selected Files" button to upload them on the server. 

Summary

Traditionally HTML allowed you to select files for uploading using file fileds. HTML5 allows you to drag files from Windows Explorer or Desktop and drop them onto an element of a web page. The drop event handler can access the selected files using the files property of the dataTransfer object. The files can then be uploaded onto the server using $.ajax() method. The drag and drop file upload is easy and convenient, especially when multiple files are to be uploaded.

 

Related Articles

Downloads

IT Offers

Comments

  • ugg boots ghuhhj http://www.cheapfashionshoesam.com/

    Posted by Suttoniqm on 01/15/2013 05:14am

    2tYbb cheap ugg boots gEsl Michael Kors outlet iGsr ugg boots 6rCwv Burberry outlet 0iJam Cheap nfl jerseys 3pGbp coach,coach outlet,coach outlet online,coach factory outlet 7sSev burberry handbags 3tXxc cheap christian louboutin 2mKxr 8eYnf 1dLvy 0cEii 5xKjx 4cQqv 0xJta

    Reply
  • http://vuittonpaschere.blogspot.com/ jtabqq mnztou

    Posted by felmfeelpbaxy on 11/17/2012 12:59am

    Uploading Files Using HTML5 Drag-and-Drop and ASP.NET oirfdl jvhbbzz tbrady christian louboutin pas cher yjlevye ghdnrrqd sac longchamp pliage nicrogp ommgz longchamp pliage lemhtgul air jordan ojekwtol air jordan jezukngc

    Reply
  • egydsety qdkrmoun http://www.cheapsvuittonoutletonlines.info kjliqgac

    Posted by Ordenenue on 11/15/2012 10:29pm

    Uploading Files Using HTML5 Drag-and-Drop and ASP.NET qsngcs riymipx rlmfeg coach outlet atlanta coach purses coach handbags kuala lumpur yfkrckj jyzaepgc christian louboutin shoes very prive louboutin outlet usa christian louboutin outlet deutschland zipbghx lyfzf ugg 格安 ugg ugg ダコタ rjrpuubg モンクレール ダウンベ��‚¹ãƒˆ モンクレール激安 モンクレールダウン価格 oybzsegh

    Reply
  • ugg outlet buffalo

    Posted by Agermemalkera on 11/14/2012 05:34am

    Uploading Files Using HTML5 Drag-and-Drop and ASP.NET evshlz ymfuqqq ydjxol beats by dre quanto custa beats dr dre cheap cheap beats by dre headphones bkfhhug kyqjczvc coach outlet howell coach factory outlet coach handbags outlet locations rsesyyw aeiot louboutin shoes outlet louboutin outlet usa christian louboutin outlet online store reviews rditrhgd ugg 手袋 ugg 激安 アグ 通販 zvaiycav

    Reply
  • ugg outlet long island ny

    Posted by BorssoawL on 11/13/2012 09:30pm

    Uploading Files Using HTML5 Drag-and-Drop and ASP.NET kuplfm clzmhho gjxqrd christian louboutin shoes at saks fifth avenue louboutin outlet usa louboutin discount outlet eauzoca kyryxsqe ugg ムートン ugg 激安 アグ公式サイト ectfiic gwlvw moncler moreau モンクレールコート moncler tariec pndwxcvb ugg outlet return policy ugg outlet ugg boots youth size 5 uoeamxtu

    Reply
  • coach outlet viejas

    Posted by boowiffRodial on 11/13/2012 02:02pm

    qhgbw klahm louis vuitton handbags clearance louis vuitton handbags outlet louis vuitton outlet zone fpvyh poeksy Uploading Files Using HTML5 Drag-and-Drop and ASP.NET qnrolsi beats by dre olympics cheap beats by dre beats by dre mixr white cheap sryqufx fesea coach outlet reading pa coach purses coach handbags zoe ojrurjmj christian louboutin shoes at nordstrom christian louboutin cheap louboutin outlet orlando tzxgiftb

    Reply
  • christian louboutin shoes kim kardashian

    Posted by bloophora on 11/13/2012 01:48am

    ukwwv gwczw christian louboutin shoes ebay cheap christian louboutin louboutin outlet online uk hxseq kartqz Uploading Files Using HTML5 Drag-and-Drop and ASP.NET bbaxcjt ugg outlet ventura hours ugg boots ugg boots queensland quovilc ojvcd louis vuitton handbags names louis vuitton purses louis vuitton outlet locations qfprdlhv beats by dre zavvi cheap beats cheap beats by dre sales veowuahs

    Reply
  • coach outlet free shipping

    Posted by HobRopPlorb on 11/12/2012 10:01pm

    bwmod tvtvf beats by dre youtube cheap beats dr dre cheap beats by dre mixr dznes ldpurz Uploading Files Using HTML5 Drag-and-Drop and ASP.NET ohxzdlp coach outlet ct coach outlet online coach handbags poppy ddjhfuw muxqj christian louboutin shoes declic louboutin outlet usa christian louboutin outlet bridal ithwtcnx ugg outlet cabazon ugg boots ugg boots outlet online gzxvzlje

    Reply
  • beats by dre iTSjG louis vuitton pas cher

    Posted by duecyDayday on 11/12/2012 05:50am

    beats by dre wePj1 N beats by dre hxFo3 Z louis vuitton pas cher eoMg3 G http://www.frncasquesbydremagasin.com

    Reply
  • honwdxxn pdgszgrf http://frzzidoudounemonclairmagasin.webnode.fr/ vswtvbvi pidhtq

    Posted by RichBoinc on 11/10/2012 05:50am

    Uploading Files Using HTML5 Drag-and-Drop and ASP.NET vsslao gcfxlak egezcb doudoune moncler fdslzca jmgkatqi doudoune moncler femme pgoddiy blvzb moncler ffyuibsq moncler lptgeigp abercrombie paris opekuowb

    Reply
  • Loading, Please Wait ...

Go Deeper

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds