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.
Step 2Become 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.
Footstep 3A Zip file will be downloaded. Unzip this file and copy the complete folder to your projection.
Pace 4Add a Generic Handler to the project and proper name it UploadHandler.
Step 5Add a binder to the project and name it UploadedFiles.
Step half dozenWrite the following code in UploadHandler.cs.
- using System.Spider web;
- namespace MulFileUpJqueryASPHandler
- {
- public form UploadsHandler : IHttpHandler
- {
- public void ProcessRequest(HttpContext context)
- {
- if (context.Asking.Files.Count > 0)
- {
- HttpFileCollection UploadedFilesCollection = context.Request.Files;
- for ( int i = 0; i < UploadedFilesCollection.Count; i++)
- {
- Organisation.Threading.Thread.Sleep(2000);
- HttpPostedFile PostedFiles = UploadedFilesCollection[i];
- cord FilePath = context.Server.MapPath( "~/UploadedFiles/" + System.IO.Path.GetFileName(PostedFiles.FileName));
- PostedFiles.SaveAs(FilePath);
- }
- }
- }
- public bool IsReusable
- {
- get
- {
- return false ;
- }
- }
- }
- }
Stride 7Add a Webform to the project and proper noun information technologyDemo.aspx.
Step 8Add the references of the following selected files in the HTML of the Demo.aspx.
Step ixAdd the following code to the HTML.
- <%@ Page Language= "C#" AutoEventWireup= "true" CodeBehind= "Demo.aspx.cs" Inherits= "MulFileUpJqueryASPHandler.Demo" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head runat="server" >
- <title>jQuery Multiple File Upload</title>
- <script src="jQuery%20Package/jquery-2.i.four.js" ></script>
- <link href="jQuery%20Package/jquery-ui.css" rel= "stylesheet" />
- <script src="jQuery%20Package/jquery-ui.js" ></script>
- <link href="jQuery%20Package/jquery-ui.structure.css" rel= "stylesheet" />
- <link href="jQuery%20Package/jquery-ui.theme.css" rel= "stylesheet" />
- <script type="text/javascript" >
- $(document).ready(function () {
- $('#btnUpload' ).click(function ( event ) {
- var uploadedFiles = $('#uploader' )[0].files;
- if (uploadedFiles.length > 0) {
- var formData =new FormData();
- for (var i = 0; i < uploadedFiles.length; i++) {
- formData.append(uploadedFiles[i].name, uploadedFiles[i]);
- }
- }
- var progressbarLabel = $('#progressbar-label' );
- var progressbarDiv = $('#progress-bar' );
- $.ajax
- ({
- url:'UploadsHandler.ashx' ,
- method:'mail' ,
- contentType:false ,
- processData:false ,
- information: formData,
- success: part () {
- progressbarLabel.text('Uploaded Successfully' );
- progressbarDiv.fadeOut(2000);
- },
- error: office (err) {
- alert(err.statusText);
- }
- });
- progressbarLabel.text('Delight Expect...' );
- progressbarDiv.progressbar({
- value:false
- }).fadeIn(m);
- });
- });
- </script>
- </head>
- <trunk>
- <form id="form1" runat= "server" >
- <div>
- Select the files to be uploaded:
- <asp:FileUpload ID="uploader" runat= "server" AllowMultiple= "true" />
- <br />
- <br />
- <input type="button" id= "btnUpload" value= "Upload At present" />
- </div>
- <div style="width:500px" >
- <div id="progress-bar" style= "position: relative; brandish: none" >
- <span id="progressbar-characterization" style= "position: absolute; left: 30%; peak: 20%;" >Delight Wait...</span>
- </div>
- </div>
- </course>
- </torso>
- </html>
Footstep 10Printing F5 to run the project and you volition see the post-obit screenshots similar an interface.
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.
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