public override void Post(HttpRequest Request, HttpResponse Response, params string[] PathParams) { Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.ContentType = @"text/plain"; // IE<=9 can't parse JSON when content type is any different than text/plain string sub_folder = "Product"; string fn = null; if (Request.Files.Count == 1) { if (!IsAcceptedImageExtension(Request.Files[0].FileName)) { RespondUnauthorized(Response); } fn = MediaUtility.SaveFile(Request.Files[0], sub_folder, 0); if (string.IsNullOrEmpty(fn)) { RespondInternalServerError(Response); } } Int64 ProductId = Request.Form["product_id"] != null?Convert.ToInt64(Request.Form["product_id"]) : 0; Int32 w = Request.Form["w"] != null?Convert.ToInt32(Request.Form["w"]) : 0; Int32 h = Request.Form["h"] != null?Convert.ToInt32(Request.Form["h"]) : 0; if (sub_folder == @"Product") { Product p = Product.FetchByID(ProductId); MediaUtility.DeleteImageFilePath(sub_folder, p.ProductImage, w, h, 0); p.ProductImage = fn; p.Save(); } if (!string.IsNullOrEmpty(fn)) { using (StreamWriter streamWriter = new StreamWriter(Response.OutputStream)) { using (JsonTextWriter jsonWriter = new JsonTextWriter(streamWriter)) { jsonWriter.WriteStartObject(); jsonWriter.WritePropertyName("file_name"); jsonWriter.WriteValue(fn); jsonWriter.WritePropertyName("preview"); jsonWriter.WriteValue(MediaUtility.GetImageFilePath(sub_folder, HttpUtility.UrlEncode(fn), 64, 64)); jsonWriter.WritePropertyName("url"); jsonWriter.WriteValue(MediaUtility.GetOriginalFilePath(sub_folder, HttpUtility.UrlEncode(fn))); jsonWriter.WriteEndObject(); } } } else { RespondInternalServerError(Response); } }
public void HandleImageRequest(HttpRequest Request, HttpResponse Response, bool IsHead, Int64 AppUserId, string SubFolder) { Int32 w, h; Int32.TryParse(Request.QueryString[@"w"] ?? "", out w); Int32.TryParse(Request.QueryString[@"h"] ?? "", out h); string image = Request.QueryString[@"image"]; if (string.IsNullOrEmpty(image)) { Response.StatusCode = 404; return; } string path = null; if (w <= 0 && h <= 0) { path = MediaUtility.GetOriginalFilePath(SubFolder, image, (SubFolder == @"Animal" ? AppUserId : 0)); } else { path = MediaUtility.GetImageFilePath(SubFolder, image, w, h, (SubFolder == @"Animal" ? AppUserId : 0)); } if (path.Length > 0 && File.Exists(path)) { Response.Cache.SetMaxAge(TimeSpan.FromDays(30)); Response.Cache.SetCacheability(HttpCacheability.Public); Response.ClearContent(); string mimeType = @"image"; if (image.EndsWith(@".jpg", StringComparison.OrdinalIgnoreCase)) { mimeType = @"image/jpg"; } else if (image.EndsWith(@".jpeg", StringComparison.OrdinalIgnoreCase)) { mimeType = @"image/jpeg"; } else if (image.EndsWith(@".png", StringComparison.OrdinalIgnoreCase)) { mimeType = @"image/png"; } else if (image.EndsWith(@".gif", StringComparison.OrdinalIgnoreCase)) { mimeType = @"image/gif"; } else if (image.EndsWith(@".mp4", StringComparison.OrdinalIgnoreCase)) { mimeType = @"video/mp4"; } else if (image.EndsWith(@".mpg", StringComparison.OrdinalIgnoreCase)) { mimeType = @"video/mpeg"; } Response.ContentType = mimeType; Response.AddFileDependency(path); Response.Cache.SetLastModifiedFromFileDependencies(); Response.Cache.SetETagFromFileDependencies(); if (IsHead) { Response.AddHeader(@"Content-Length", new FileInfo(path).Length.ToString()); } else { Response.TransmitFile(path); } return; } else { Response.StatusCode = 404; return; } }