Redactor upload images – .NET C# Handler
Redactor is an amazing text editor, super simple, highly customized with a clean design, you can implement in your website, with limited hussle. Here is a simple Handler you can add to you ASP.NET website to handle image uploads.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
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; } } } |