17 August 2010

Asynchronous File Upload in Asp.net using C# jquery Uploadify

Written by Amit Panchal, Posted in Blog

Hello Readers,
You might have a need for uploading files asynchronously in asp.net using c# with jquery.
here i will give u the best tutorial to do this.
You will need to download the jquery pluggin this.download this pluggin from here.
http://www.uploadify.com/download/
after u download this file extract this folder and rename it to uploadify and put it in the root folder of ur project.
Now crate new aspx page from which we will upload file. name it ImageUploader.aspx(you can give any name)
insert the following code in ur header part of ur file.


 
    
 
    
 
    
 
//reference of css file of uploadify, you will find this in the folder  uploadify

and then in the body of ur page insert the following code...


   

Now let's create the handler to receive the httpPostedData.
Add Generic Handler to ur project(Right click on project from solution explorer and then click on add new item.from the given option select Generic Handler). Name it Uploader.ashx
The code for the handler is as below.

using System;
using System.Web;
using System.Web.SessionState;
 
public class Uploader : IHttpHandler, IRequiresSessionState
{
     
    public void ProcessRequest (HttpContext context) {
        try
        {
            HttpPostedFile file = context.Request.Files["Filedata"];
            int id = (Int32.Parse(context.Request["id"]));//here we are accessing the passed values from the javascript
            string filename = id.ToString() + file.FileName;
            string filepath = HttpContext.Current.Server.MapPath("~").ToString() + "\\Avatar\\"+filename ;
            file.SaveAs(filepath);
            //your asp.net logic to save file path in database
            context.Response.Write("1");
        }
        catch (Exception ex)
        {
            context.Response.Write("0");
        }
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }
 
} 

It's done just run ur imageupload.aspx page and click on browse button to upload the file.
if u face any problem feel free to comment.

Social Bookmarks

About the Author

Amit Panchal

Co-Founder,

Amitech Business Solutions

All Events

Textify

Comments (3)

  • Otto Kristianson
    19 November 2011 at 15:06 |

    Many thanks for your information! I really enjoyed it.

  • Newton Kisch
    25 November 2011 at 14:54 |

    I like the style of your website! It looks really great.

  • Taro
    20 February 2012 at 22:02 |

    can u help how iresnt all info into mysql db especially i have 2 table : 1st table for user information 2nd table for files attachments, 2 tables related each others because user information tables have 2 columns to retrieve attachments from 2nd tables ( all attachments file info store here size, type, name , tmp_name).user_info (table 1 structure)id(PK)user nameuser_birthuser_attachements (files 1)user_approval (files 2)..createdfile attachmentsid(PK)nametypesizetemp_name tq sorry about my english fairuz s last blog ..

Leave a comment

You are commenting as guest. Optional login below.

Cancel Submitting comment...