Show Error Msg in Mutiple File Upload Jquery

  • Updated date Jul 06, 2015
  • 53.6k
  • three

In this article you will learn how to upload multiple files using jQuery AJAX and jQueryUI Progressbar with ASP.NET Generic Handler.

Objective

Our master objective is to upload multiple files at one time and salve them in a local binder. Also, we want to show a progress bar until the uploading is consummate with a message to prompt the user that the files take been uploaded successfully.

Method

To do it, we volition apply an ASP.NET generic HTTP handler to get the files and salvage them to the local folder. Using jQuery Ajax nosotros will pass the files to the handler using the post request. Also, to show the processing, nosotros will be using jQueryUI Progressbar. Now, allow'south see how to practice this.

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

web application

Step 2
Become to jqueryui.com and select the download tab. Then, click the Progressbar checkbox and click the download push button at the bottom of the page.

download builder

widgets

download

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

jQuery package

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

UploadHandler

Step 5Add a binder to the project and name it UploadedFiles.

UploadedFiles

Step half dozenWrite the following code in UploadHandler.cs.

  1. using  System.Spider web;
  2. namespace  MulFileUpJqueryASPHandler
  3. {
  4. public form  UploadsHandler : IHttpHandler
  5.     {
  6. public void  ProcessRequest(HttpContext context)
  7.         {
  8. if  (context.Asking.Files.Count > 0)
  9.             {
  10.                 HttpFileCollection UploadedFilesCollection = context.Request.Files;
  11. for  ( int  i = 0; i < UploadedFilesCollection.Count; i++)
  12.                 {
  13.                     Organisation.Threading.Thread.Sleep(2000);
  14.                     HttpPostedFile PostedFiles = UploadedFilesCollection[i];
  15. cord  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. }

Stride 7Add a Webform to the project and proper noun information technologyDemo.aspx.

add webform

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

Step ixAdd 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.i.four.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:'mail' ,
  27.                         contentType:false ,
  28.                         processData:false ,
  29.                         information: formData,
  30.                         success: part () {
  31.                             progressbarLabel.text('Uploaded Successfully' );
  32.                             progressbarDiv.fadeOut(2000);
  33.                         },
  34.                         error: office (err) {
  35.                             alert(err.statusText);
  36.                         }
  37.                     });
  38.                 progressbarLabel.text('Delight Expect...' );
  39.                 progressbarDiv.progressbar({
  40.                     value:false
  41.                 }).fadeIn(m);
  42.             });
  43.         });
  44.     </script>
  45. </head>
  46. <trunk>
  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 At present"  />
  54.         </div>
  55.         <div style="width:500px" >
  56.             <div id="progress-bar"  style= "position: relative; brandish: none" >
  57.                 <span id="progressbar-characterization"  style= "position: absolute; left: 30%; peak: 20%;" >Delight Wait...</span>
  58.             </div>
  59.         </div>
  60.     </course>
  61. </torso>
  62. </html>

Footstep 10Printing F5 to run the project and you volition see the post-obit screenshots similar an interface.

file uoload

output

Explanation

UploadHandler.cs

  • First we are checking, that if the user has uploaded at least 1 file.
  • We are saving the uploaded files in the HttpFileCollection Class's object.
  • Using a for loop, nosotros are iterating through each file uploaded by the user.
  • Saving each file in the HttpPostedFile Object using the alphabetize values of each uploaded file.
  • We are saving the filename with its path using the Server.MapPath method of and FileName Property in a string variable.
  • Finally we saved the file in the HttpPostedFile Object using the SaveAs method.

Demo.aspx

  • Add together a file upload and a button command from toolbox.
  • To brandish the progressbar nosotros have created a div with 500px width.
  • We have added an inline CSS making the position relative and display is none, because, we don't want to evidence the bar when the page first loads.
  • A span is added to show the text, Please Look and its position is taken as absolute and information technology is relative to the parent div.

jQuery Code

  • On the click event of the button control, the files uploaded in the uploader are saved in a local variable.
  • And then nosotros appended the formData by iterating through each file uploaded.
  • At present, using the Ajax options, we will send the data to the handler.
  • The URL is the Handler, the requestType is Mail and the data is coming from formData. When it succeeds, we changed the span text to uploaded successfully and fade out in 2 seconds.
  • If an fault exists, it is shown in a JavaScript alarm.
  • And when the folio again loads, we changed the text to Delight Wait and Fade in property to i seconds. Also its value is imitation, to tell the progressbar that we will pass the data using our own customizations.

dysartbegue1972.blogspot.com

Source: https://www.c-sharpcorner.com/UploadFile/da55bf/multiple-files-upload-using-jquery-ajax-and-jqueryui-progres/

0 Response to "Show Error Msg in Mutiple File Upload Jquery"

Enviar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel