using System;
using System.Web;
using System.Web.Configuration;
using System.IO;
using System.Collections.Generic;
public class ImageUploadHandler : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "multipart/form-data";
context.Response.Expires = -1;
string UploadImageFolderUrl = "/_resources/images/";
try
{
HttpPostedFile postedFile = context.Request.Files["file"];
string savepath = HttpContext.Current.Server.MapPath(UploadImageFolderUrl);
var extension = Path.GetExtension(postedFile.FileName);
System.Web.Script.Serialization.JavaScriptSerializer javascriptSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
Dictionary<string, string> files = new Dictionary<string, string>();
if (!Directory.Exists(savepath))
Directory.CreateDirectory(savepath);
var id = Guid.NewGuid() + extension;
if (extension != null)
{
var fileLocation = string.Format("{0}{1}",
savepath,
id);
files.Add("filelink", UploadImageFolderUrl + id);
postedFile.SaveAs(fileLocation);
context.Response.ContentType = "application/json";
context.Response.Write(javascriptSerializer.Serialize(files));
context.Response.StatusCode = 200;
}
}
catch (Exception ex)
{
context.Response.Write("Error: " + ex.Message);
}
}
public bool IsReusable
{
get
{
return false;
}
}
}