Wednesday, September 15, 2010

Uploading File with progress bar In Asp.net

Uploading file is very common task in Asp.net applications. Although asp.net evolve pretty much around time but file upload control is same from 1.0 to 3.5. Web Apps are becoming more and more interactive these days. in this scenario uploading file with progress bar and without post back give user feed back about status of task.
I find a very nice Jquery plug in called Uploadify for this. You can download it from here.
They have starting example on the site a PHP code snippet. I just want to share my Asp.net Implementation.

First of all download Jquery and Uploadify plug in the create a Asp.net website using Visual Studio or Visual Web Developer and copy the "uploadify" folder to your web site. Now Add reference of following files to your aspx page. like this



Now include the reference to uploadify.css file. otherwise it will not display as you expect.
Now add this script to you aspx page
Finally Add a Generic handler (see the Handler.ashx in front of 'script' property of uploadify)

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

using System;
using System.Web;

public class Handler : IHttpHandler {

public void ProcessRequest (HttpContext context) {

string path = string.Empty;

for (int j = 0; j < uploadfile =" context.Request.Files[j];" path =" context.Server.MapPath("> 0)
{
uploadFile.SaveAs(path);

}
}


}

public bool IsReusable {
get {
return false;
}
}

}
you can read detail of uploadify function and its properties from here

Now run the page and it will look like image below.


Happy coding :)

5 comments:

  1. Thank you Bhatti saab, very informative indeed . I am trying to make asp.net insert form with this upload, once i am done i will share..thanks again and keep writing.

    ReplyDelete
  2. @Abdul Rehman
    Thanks for nice words. I am glad you found the post helpful. I always try to share solution of problems i face during development.

    ReplyDelete
  3. Hi what is this line of code

    uploadFile.SaveAs(path);

    which is not refrenced anywhere.

    Can u post the whole code.

    Thanks

    ReplyDelete
  4. please view http://waqarsherbhatti.posterous.com/ for latest posts... And about the line of code you are asking... uploadFile is Asp.net FileUpload Control... which is in Aspx. file...

    ReplyDelete