Saturday 27 August 2011

Upload file using ajax file uploader

/*In this article you will learn how to upload file using ajax file uploader javascript library in json.This is very simple and easy to use this script file. You only need to add reference script libray into your aspx page.*/
//below is the aspx code for uploading files.





//Add reference for ajax file upload library. 



//Create Handler to save file  and write below code inside Process Request
public void ProcessRequest (HttpContext context) {
        try
        {
            string Attachment = "";
            string attachmentPath = "";            
            context.Response.ContentType = "text/plain";
            context.Response.Expires = -1;
            string pathToSave = context.Server.MapPath(@"~\FolderPath\");
            string fileName = string.Empty;
            string fextention = string.Empty;

                foreach (string f in context.Request.Files.AllKeys)
                {
                    HttpPostedFile file = context.Request.Files[f];
                    if (!String.IsNullOrEmpty(file.FileName))
                    {

                        Attachment = file.FileName;
                        Attachment = Attachment.Replace(" ", "");
                        Attachment = Attachment.Replace("'", "");
                        
                        Attachment = pathToSave + Attachment;
                          
                        
                        file.SaveAs(Attachment);

                    }

                }
            

        }
        catch (Exception ex)
        {
            context.Response.Write("Error: " + ex.Message);
        }
    }

No comments:

Post a Comment