In this article, we will look into file upload using JQuery plug-in. Ajax file upload plug-in allows us to upload files to server. I am going to explain it with an example. We will be using custom HTTP handler to upload file onto the server. Create an ASP.NET MVC web application and name it as JQueryFileUpload. Now, add below scripts to site.master:
- <script src="../../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
- <script src="../../Scripts/ajaxupload.js" type="text/javascript"></script>
- <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
- <div id="uploadStatus" style="color: red;"></div>
- <input type="button" id="uploadFile" value="Upload File" />
- <div id="fileList"></div>
- </asp:Content>
- public class FileUploadHandler: IHttpHandler
- {
- #region IHttpHandler Members
- public bool IsReusable {
- get {
- return false;
- }
- }
- public void ProcessRequest(HttpContext context) {
- try {
- //Uploaded File Deletion
- if (context.Request.QueryString.Count > 0) {
- string filePath = @ "c:\DownloadedFiles\" + context.Request.QueryString[0].ToString();
- if (File.Exists(filePath))
- File.Delete(filePath);
- }
- //File Upload
- else {
- string fileName = Path.GetFileName(context.Request.Files[0].FileName);
- string location = @ "c:\DownloadedFiles\" + fileName;
- context.Request.Files[0].SaveAs(location);
- }
- } catch {}
- }#endregion
- }
- <add path="*.upload" verb="*" type="JQueryFileUpload.FileUploadHandler"/>
- <script type="text/javascript">
- $(function() {
- //Function to upload file.
- new AjaxUpload('#uploadFile',
- {
- action: 'FileUploadHandler.upload',
- name: 'upload',
- onComplete: function(file) {
- $('<div><img src="../../Content/delete.jpg" style="width:20px;height:20px" class="delete"/>' + file + '</div>').appendTo('#fileList');
- $('#uploadStatus').html("Upload Done.");
- },
- onSubmit: function(file, ext) {
- if (!(ext && /^(txt|doc|docx|xls|pdf)$/i.test(ext))) {
- alert('Invalid File Format.');
- return false;
- }
- $('#uploadStatus').html("Uploading...");
- }
- });
- //Function to delete uploaded file in server.
- $('img').live("click", function() {
- $('#uploadStatus').html("Deleting");;
- deleteFile($(this));
- });
- });
- function deleteFile(objfile) {
- $.ajax({
- url: 'FileUploadHandler.upload?del=' + objfile.parent().text(),
- success: function() {
- objfile.parent().hide();
- }
- });
- $('#uploadStatus').html("Deleted");
- }
- </script>
- routes.IgnoreRoute("{file}.upload");
I am ending the things here. I am attaching the source code with it. I hope this article will be helpful for all.

Nivedita YadavPosted Jan 3, 2023, 8:04 AM
Ajaxupload.js:581 Uncaught DOMException: Blocked a frame with origin "https://localhost:44361" from accessing a cross-origin frame. at HTMLIFrameElement.<anonymous> (https://localhost:44361/admin/JavaScript/ajaxupload.js:581:102)
Nivedita YadavPosted Jan 3, 2023, 8:04 AM
Ajaxupload.js is not working if https is installed in the URL, works without https, any help
Hanif HefazPosted Jul 23, 2016, 7:45 AM
Interesting
Vivek TripathiPosted Jul 21, 2016, 11:42 AM
Nice
kalu singh raoPosted Jul 8, 2016, 1:26 AM
Nice...
Sr KarthigaPosted Feb 26, 2016, 9:32 AM
nice explanation
Sr KarthigaPosted Feb 18, 2016, 7:43 PM
GOOD ONE
Abhishek KumarPosted Oct 12, 2015, 2:07 AM
Thanks a lot for nice article sr..
vijayPosted Jun 27, 2014, 9:24 AM
How to restrict file size in this example
majid ghodsPosted Sep 9, 2013, 7:57 AM
thanks.........................................
Brahmadev bcodPosted Jul 24, 2013, 8:08 AM
how To limit the file before uploading
Former memberPosted Apr 4, 2013, 3:55 AM
Hi , it's not working. it's causing post back
bassam mohPosted Jul 12, 2012, 6:02 PM
I download the source but is really not good
BASKER RAJAVELPosted Mar 14, 2012, 7:38 AM
I want Javascript with Html File only.
Roy TynanPosted Feb 11, 2011, 9:28 AM
Poor code
Richard NorthcottPosted Jan 20, 2011, 1:15 PM
when i run it on a proper server (iis) it fails to save the file or see any bytes in the context.Request.InputStream (which was to be a way of holding the file before the user saved it at which point it would be saved to the database). am i having this problem because I don't have an MVC app (and haven't been able to add the routes code to my Global.asax file? Thanks again and I'm hoping this can be made to work as it's great.
Vivek KandagatlaPosted Oct 5, 2010, 7:47 AM
Hi Author, Could you please let me know how can I get the file size of the respective file which we are uploading using your code. Thanks, Vivek
saba perveenPosted Jun 21, 2010, 7:25 AM
After download this file 'jqueryfileupload.zip' then it not opens in vs 2008. why?????? plz tell me...
suradech paleePosted Apr 4, 2010, 11:04 PM
thanks for knowledge. toyota 4runner lift kit i interating in jquery and i'm just know in it little bit.