Objective

Our main objective is to upload multiple files at once and save them in a local folder. Also, we want to show a progress bar until the uploading is complete with a message to prompt the user that the files have been uploaded successfully.

Method

To do it, we will use an ASP.NET generic HTTP handler to get the files and save them to the local folder. Using jQuery Ajax we will pass the files to the handler using the post request. Also, to show the processing, we will be using jQueryUI Progressbar. Now, let's see how to do this.

Step 1

Create an empty ASP.NET web application in Visual Studio IDE.

web application

Step 2

Go to jqueryui.com and select the download tab. Then, click the Progressbar checkbox and click the download button at the bottom of the page.

download builder

widgets

download

Step 3

A Zip file will be downloaded. Unzip this file and copy the complete folder to your project.

jQuery package

Step 4

Add a Generic Handler to the project and name it UploadHandler.

UploadHandler

Step 5

Add a folder to the project and name it UploadedFiles.

UploadedFiles

Step 6

Write the following code in UploadHandler.cs.

  1. using System.Web;
  2. namespace MulFileUpJqueryASPHandler
  3. {
  4. public class UploadsHandler : IHttpHandler
  5. {
  6. public void ProcessRequest(HttpContext context)
  7. {
  8. if (context.Request.Files.Count > 0)
  9. {
  10. HttpFileCollection UploadedFilesCollection = context.Request.Files;
  11. for (int i = 0; i < UploadedFilesCollection.Count; i++)
  12. {
  13. System.Threading.Thread.Sleep(2000);
  14. HttpPostedFile PostedFiles = UploadedFilesCollection[i];
  15. string FilePath = context.Server.MapPath("~/UploadedFiles/" + System.IO.Path.GetFileName(PostedFiles.FileName));
  16. PostedFiles.SaveAs(FilePath);
  17. }
  18. }
  19. }
  20. public bool IsReusable
  21. {
  22. get
  23. {
  24. return false;
  25. }
  26. }
  27. }
  28. }
Step 7

Add a Webform to the project and name it Demo.aspx.

add webform

Step 8

Add the references of the following selected files in the HTML of the Demo.aspx.

Add the references

Step 9

Add the following code to the HTML.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Demo.aspx.cs" Inherits="MulFileUpJqueryASPHandler.Demo" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title>jQuery Multiple File Upload</title>
  6. <script src="jQuery%20Package/jquery-2.1.4.js"></script>
  7. <link href="jQuery%20Package/jquery-ui.css" rel="stylesheet" />
  8. <script src="jQuery%20Package/jquery-ui.js"></script>
  9. <link href="jQuery%20Package/jquery-ui.structure.css" rel="stylesheet" />
  10. <link href="jQuery%20Package/jquery-ui.theme.css" rel="stylesheet" />
  11. <script type="text/javascript">
  12. $(document).ready(function () {
  13. $('#btnUpload').click(function (event) {
  14. var uploadedFiles = $('#uploader')[0].files;
  15. if (uploadedFiles.length > 0) {
  16. var formData = new FormData();
  17. for (var i = 0; i < uploadedFiles.length; i++) {
  18. formData.append(uploadedFiles[i].name, uploadedFiles[i]);
  19. }
  20. }
  21. var progressbarLabel = $('#progressbar-label');
  22. var progressbarDiv = $('#progress-bar');
  23. $.ajax
  24. ({
  25. url: 'UploadsHandler.ashx',
  26. method: 'post',
  27. contentType: false,
  28. processData: false,
  29. data: formData,
  30. success: function () {
  31. progressbarLabel.text('Uploaded Successfully');
  32. progressbarDiv.fadeOut(2000);
  33. },
  34. error: function (err) {
  35. alert(err.statusText);
  36. }
  37. });
  38. progressbarLabel.text('Please Wait...');
  39. progressbarDiv.progressbar({
  40. value: false
  41. }).fadeIn(1000);
  42. });
  43. });
  44. </script>
  45. </head>
  46. <body>
  47. <form id="form1" runat="server">
  48. <div>
  49. Select the files to be uploaded:
  50. <asp:FileUpload ID="uploader" runat="server" AllowMultiple="true" />
  51. <br />
  52. <br />
  53. <input type="button" id="btnUpload" value="Upload Now" />
  54. </div>
  55. <div style="width:500px">
  56. <div id="progress-bar" style="position: relative; display: none">
  57. <span id="progressbar-label" style="position: absolute; left: 30%; top: 20%;">Please Wait...</span>
  58. </div>
  59. </div>
  60. </form>
  61. </body>
  62. </html>
Step 10

Press F5 to run the project and you will see the following screenshots like an interface.

file uoload

output

Explanation

UploadHandler.cs

Demo.aspx

jQuery Code