Learning Hub..!

Prabhakaran J – Blog

ASP.NET

Uploading Files using JQuery Ajax in ASP.NET

Simply way we can upload the images from ASP.NET form using Jquery Ajax, User can upload the file to server through Ajax Request to Generic Handler.

Web From: 

JQuery:

$(function () {
$("#btnregister").click(function () {
SaveImage();
});
});

function SaveImage() {
var fileUpload = $("#filepro").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: "uploadimages.ashx",
type: "POST",
data: data,
contentType: false,
processData: false,
success: function (result) { alert(result); },
error: function (err) {
alert(err.statusText)
}
});

evt.preventDefault();
}

uploadimages.ashx

<%@ WebHandler Language="C#" Class="uploadimages" %>

using System;
using System.Web;

public class uploadimages : 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("~/userimages/" + file.FileName);
file.SaveAs(fname);
}
context.Response.ContentType = "text/plain";
context.Response.Write("Image Uploaded successfully!");
}
}

public bool IsReusable {
get {
return false;
}
}

}

6 Comments

  1. minecraft

    I’ve read some good stuff here. Definitely value bookmarking for revisiting.

    I wonder how so much effort you set to make this sort of magnificent
    informative site.

  2. gamefly

    continuously i used to read smaller content that also clear their motive, and that is also
    happening with this article which I am reading here.

  3. Pamala

    It’s truly very complicated in this busy life to listen news on TV, so
    I simply use world wide web for that purpose, and get the newest information.

  4. SEO SERVICES

    I loved your article post.Really thank you! Much obliged.

  5. Kerstin

    This is really attention-grabbing, You are
    an overly skilled blogger. I’ve joined your feed and sit up for seeking extra of your fantastic post.

    Also, I’ve shared your web site in my social networks

  6. Great, thanks for sharing this post.Really looking forward to read more. Really Great.

Leave a Reply

Prabhakaran Jayaraman