Introduction:
Here I will explain how to upload images or files using jQuery without page refresh or postback in asp.net using handler (ashx) in c#, vb.net or jQuery upload files or images without postback in asp.net using Handler in c#, vb.net or asynchronous file upload using jQuery without submitting form in asp.net with c#, vb.net.
Description:
In previous articles I explained jQuery call asp.net page methods from json, 7 + jQuery multiple fileupload plugin examples, jQuery check file size before upload in asp.net, jQuery setInterval() function example, jQuery upload multiple files using uplodify plugin in asp.net and many articles relating to jQuery, JavaScript and asp.net. Now I will explain how to upload images or files using jQuery without page refresh or postback in asp.net using handler (ashx) in c#, vb.net.
In previous articles I explained jQuery call asp.net page methods from json, 7 + jQuery multiple fileupload plugin examples, jQuery check file size before upload in asp.net, jQuery setInterval() function example, jQuery upload multiple files using uplodify plugin in asp.net and many articles relating to jQuery, JavaScript and asp.net. Now I will explain how to upload images or files using jQuery without page refresh or postback in asp.net using handler (ashx) in c#, vb.net.
To upload images or files using jQuery without page refresh or postback in asp.net using handler (ashx) first create the new web application and open Default.aspx page and write the following code
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>uploading file using
jquery with generic handler ashx</title>
<script src="http://code.jquery.com/jquery-1.10.2.js"
type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$('#btnUpload').click(function () {
var fileUpload = $("#FileUpload1").get(0);
var files =
fileUpload.files;
var test = new FormData();
for (var i = 0; i < files.length; i++) {
test.append(files[i].name, files[i]);
}
$.ajax({
url: "UploadHandler.ashx",
type: "POST",
contentType: false,
processData: false,
data: test,
// dataType: "json",
success: function (result) {
alert(result);
},
error: function (err) {
alert(err.statusText);
}
});
});
})
</script>
</head>
<body>
<form id="form1"
runat="server">
<div>
<input type="file"
id="FileUpload1"
/>
<input type="button"
id="btnUpload"
value="Upload
Files"/>
</div>
</form>
</body>
</html>
|
If you observe jQuery
ajax method in above code we mentioned “UploadHandler.ashx” by using this file we will upload files in server side. Now add handler
file in your application by following below steps
Right click on your application à select Add
New Item à select Generic
Handler file à Give a name and click Add button like as shown below
Once you finished adding handler file now add new
folder in your application “uploads” that would be like as shown below
Now open UploadHandler.ashx file and write
the following code
C#
Code
|
VB.NET
Code
|
Now run your application that will allow you to
upload files in upload folder using handler files.
Demo
Download
Sample Attached
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
8 comments :
Hi Suresh,
Thanks for the nice writeup. What is the largest file size can this approach support?
-Ravi.
Hi Suresh , it is woking fine in chrome but not in IE . what to do .
thanks
Hi Suresh , it is not working in content page, what should i do?
Thanks for posting this and making source code available.
Hi, what is var test = new FormData(); i've error in FormData(), not find this function.
It's working fine inIE also
Showing 'Internal Server Error' while using it on IIS
Very helpful!
Thank you so much!
Can you please publish the download code also using json.
Thanks in advnace!
Note: Only a member of this blog may post a comment.