Saturday, March 10, 2012

Upload Multiple Images With Progress Bar Using Uploadify in asp.net

The jQuery Uploadify website gives us ready example for PHP, but leaves the new guies to C# or VB clueless on how to implement the same in .NET.

So, here I am presenting you with example of C#.NET implementation (upload.ashx) for the jQuery Uploadify plugin from a reference as shown below in master page to upload the file and crop it using jcrop with single Image in the next post :



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

using System;
using System.Web;
using System.IO;

public class Upload : IHttpHandler {
  
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        context.Response.Expires = -1;
        try
        {
            HttpPostedFile postedFile = context.Request.Files["Filedata"];
          
            string savepath = "";
            string tempPath = "";

            tempPath = context.Request["folder"];

            //If you prefer to use web.config for folder path, uncomment below line:
            //tempPath = System.Configuration.ConfigurationManager.AppSettings["FolderPath"];

          
            savepath = context.Server.MapPath(tempPath);
            string filename = postedFile.FileName;
            if (!Directory.Exists(savepath))
                Directory.CreateDirectory(savepath);

            postedFile.SaveAs(savepath + @"\" + filename);
            context.Response.Write(tempPath + "/" + filename);
            context.Response.StatusCode = 200;
        }
        catch (Exception ex)
        {
            context.Response.Write("Error: " + ex.Message);
        }
    }

    public bool IsReusable {
        get {
            return false;
        }
    }
}


 Click Here 2 Download

No comments:

Post a Comment