One of the very common task in web application is allowing user to upload files from the client machine to Server. usually files are uploaded along with a form submission. But using JQuery and Ajax we can accomplish this task without the entire page post back.In such cases, you can allow a user to select files using the FileUpload server control of ASP.NET and then upload those files to the server through an Ajax request to a generic handler.
Requirement
Requirement
- Web form
- JQuery
- Generic handler
Web form
The following web form consists of a file upload control,user can select the file using file upload control and then they click the upload button to upload the selected file to server.
- <h3>Upload File using Jquery AJAX in Asp.net</h3>
- <table>
- <tr>
- <td>File:</td>
- <td>
- <asp:FileUpload ID="fupload" runat="server" onchange='prvimg.UpdatePreview(this)' /></td>
- <td><asp:Image ID="imgprv" runat="server" Height="90px" Width="75px" /></td>
- </tr>
- <tr>
- <td></td>
- <td><asp:Button ID="btnUpload" runat="server" cssClass="button" Text="Upload Selected File" /></td>
- </tr>
- </table>
- $("#btnUpload").click(function (evt) {
- var fileUpload = $("#fupload").get(0);
- var files = fileUpload.files;
- var data = new FormData();
- for (var i = 0; i < files.length; i++) {
- data.append(files[i].name, files[i]);
- }
- $.ajax({
- url: "FileUploadHandler.ashx",
- type: "POST",
- data: data,
- contentType: false,
- processData: false,
- success: function (result) { alert(result); },
- error: function (err) {
- alert(err.statusText)
- }
- });
- evt.preventDefault();
- });
- <%@ WebHandler Language="C#" Class="FileUploadHandler" %>
- using System;
- using System.Web;
- public class FileUploadHandler : IHttpHandler
- {
- public void ProcessRequest (HttpContext context)
- {
- if (context.Request.Files.Count > 0)
- {
- HttpFileCollection files = context.Request.Files;
- for (int i = 0; i < files.Count; i++)
- {
- HttpPostedFile file = files[i];
- string fname = context.Server.MapPath("~/uploads/" + file.FileName);
- file.SaveAs(fname);
- }
- context.Response.ContentType = "text/plain";
- context.Response.Write("File Uploaded Successfully!");
- }
- }
- public bool IsReusable
- {
- get
- {
- return false;
- }
- }
- }

Satya Prakash VermaPosted Dec 1, 2022, 5:46 PM
Help full, thanks for article
Jainam ShahPosted Jun 7, 2020, 1:14 PM
Very helpful. Thanks!
Abucheri DerrickPosted Apr 2, 2019, 7:07 AM
Works well on Google chrome but fils on IE and Edge
Deepak SharmaPosted Feb 14, 2019, 1:50 AM
Although did some changes but must say it worked and well tried
BhanuChandra JamiligiriPosted Jun 17, 2017, 5:25 AM
We are getting Error in IE browser. Might be the same issue in Firefox too.
Anu VPosted Sep 9, 2016, 7:53 AM
Nice...
Kaushik HalvadiaPosted Jul 13, 2016, 10:12 AM
It will not work as UpdatePreview() java script function definition is not written in code
dario alPosted Feb 11, 2016, 3:42 PM
is it going to work with large files (100mb)?
deepak karmaPosted Jan 18, 2016, 7:26 AM
this is not working in IE8..kindly pls let me know what changes required to run on IE which not support FormData()