public JsonResult UploadImage(string fileinputname, string savepath, string oldfilename) { CustomJsonResult r = new CustomJsonResult(); r.ContentType = "text/html"; try { HttpPostedFileBase file_upload = Request.Files[fileinputname]; if (file_upload == null) { return(Json("text/html", ResultType.Failure, "上传失败")); } System.IO.FileInfo file = new System.IO.FileInfo(file_upload.FileName); if (file.Extension != ".jpg" && file.Extension != ".png" && file.Extension != ".gif" && file.Extension != ".bmp") { return(Json("text/html", ResultType.Failure, "上传的文件不是图片格式(jpg,png,gif,bmp)")); } ImageUpload s = new ImageUpload(); string domain = System.Configuration.ConfigurationManager.AppSettings["custom:Domain"]; string imagesServerUrl = CommonSetting.GetUploadPath(savepath); if (s.Save(file_upload, domain, imagesServerUrl, "")) { r.Content = s; } r.Result = ResultType.Success; r.Message = "上传成功"; } catch (Exception ex) { r.Result = ResultType.Exception; r.Message = "上传图片发生异常"; throw (ex); } return(r); }
public HttpResponseMessage Post(UploadFileEntity entity) { SetTrackID(); Log.Info("调用UploadImage"); HttpContextBase context = (HttpContextBase)Request.Properties["MS_HttpContext"]; //获取传统context HttpRequestBase request = context.Request; //定义传统request对象 CustomJsonResult r = new CustomJsonResult(); try { if (entity.FileData != null && entity.FileData.Length > 0) { ImageUpload image = new ImageUpload(); int[] bigImgSize = new int[2] { 500, 500 }; int[] smallImgSize = new int[2] { 100, 100 }; string imageSign = ""; string savefolder = CommonSetting.GetUploadPath(entity.UploadFolder); string extension = Path.GetExtension(entity.FileName).ToLower(); string yyyyMMddhhmmssfff = Guid.NewGuid().ToString(); string originalNewfilename = imageSign + yyyyMMddhhmmssfff + "_O" + extension; //原图片文件名称 string bigNewfilename = imageSign + yyyyMMddhhmmssfff + "_B" + extension; //大图片文件名称 string smallNewfilename = imageSign + yyyyMMddhhmmssfff + "_S" + extension; //小图片文件名称 string originalSavePath = string.Format("{0}/{1}", savefolder, originalNewfilename); string bigSavePath = string.Format("{0}/{1}", savefolder, bigNewfilename); string smallSavePath = string.Format("{0}/{1}", savefolder, smallNewfilename); string path = System.Configuration.ConfigurationManager.AppSettings["custom:FileServerUploadPath"]; string serverOriginalSavePath = path + "/" + originalSavePath; string serverBigSavePath = path + "/" + bigSavePath; string serverSmallSavePath = path + "/" + smallSavePath; entity.FileName = entity.FileName.ToLower().Replace("\\", "/"); ImageUpload s = new ImageUpload(); string domain = System.Configuration.ConfigurationManager.AppSettings["custom:FilesServerUrl"]; DirectoryInfo Drr = new DirectoryInfo(path + "/" + savefolder); if (!Drr.Exists) { Drr.Create(); } FileStream fs = new FileStream(serverOriginalSavePath, FileMode.Create, FileAccess.Write); fs.Write(entity.FileData, 0, entity.FileData.Length); fs.Flush(); fs.Close(); System.Drawing.Image originalImage = System.Drawing.Image.FromFile(serverOriginalSavePath); image.OriginalPath = domain + originalSavePath; image.OriginalWidth = originalImage.Width; image.OriginalHeight = originalImage.Height; if (entity.GenerateSize) { if (GreateMiniImageModel(serverOriginalSavePath, serverBigSavePath, bigImgSize[0], bigImgSize[1])) { image.BigPath = domain + bigSavePath; image.BigWidth = bigImgSize[0]; image.BigHeight = bigImgSize[1]; } if (GreateMiniImageModel(serverOriginalSavePath, serverSmallSavePath, smallImgSize[0], smallImgSize[1])) { image.SmallPath = domain + smallSavePath; image.SmallWidth = smallImgSize[0]; image.SmallHeight = smallImgSize[1]; } } originalImage.Dispose(); r.Data = image; r.Message = "上传成功"; r.Result = ResultType.Success; } } catch (Exception ex) { r.Result = ResultType.Exception; r.Message = "上传失败"; Log.Error("WebApi上传图片异常", ex); } string json = r.ToString(); HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(json, Encoding.GetEncoding("UTF-8"), "application/json") }; return(result); }
public HttpResponseMessage Post(UploadFileEntity entity) { SetTrackID(); Log.Info("调用UploadFile"); HttpContextBase context = (HttpContextBase)Request.Properties["MS_HttpContext"]; //获取传统context HttpRequestBase request = context.Request; //定义传统request对象 CustomJsonResult r = new CustomJsonResult(); try { if (entity.FileData == null) { r = new CustomJsonResult(ResultType.Failure, "文件对象为空"); } else if (entity.FileData.Length == 0) { r = new CustomJsonResult(ResultType.Failure, "文件内容为空"); } else if (entity.FileData.Length > (50 * 1024 * 1024)) { r = new CustomJsonResult(ResultType.Failure, "文件大小不能超过50M"); } else { string imageSign = ""; string savefolder = CommonSetting.GetUploadPath(entity.UploadFolder); string extension = Path.GetExtension(entity.FileName).ToLower(); string yyyyMMddhhmmssfff = Guid.NewGuid().ToString(); string originalNewfilename = imageSign + yyyyMMddhhmmssfff + extension;//原图片文件名称 string originalSavePath = string.Format("{0}/{1}", savefolder, originalNewfilename); string path = System.Configuration.ConfigurationManager.AppSettings["custom:ImagesServerUploadPath"]; string serverOriginalSavePath = path + originalSavePath; entity.FileName = entity.FileName.ToLower().Replace("\\", "/"); ImageUpload s = new ImageUpload(); string domain = System.Configuration.ConfigurationManager.AppSettings["custom:ImagesServerUrl"]; DirectoryInfo Drr = new DirectoryInfo(path + savefolder); if (!Drr.Exists) { Drr.Create(); } FileStream fs = new FileStream(serverOriginalSavePath, FileMode.Create, FileAccess.Write); fs.Write(entity.FileData, 0, entity.FileData.Length); fs.Flush(); fs.Close(); string url = domain + originalSavePath; string name = entity.FileName == null ? "" : entity.FileName; r.Data = "{\"url\":\"" + url + "\",\"name\":\"" + name + "\",\"referenceId\":\"" + entity.ReferenceId + "\"}"; r.Message = "上传成功"; r.Result = ResultType.Success; } } catch (Exception ex) { r.Result = ResultType.Exception; r.Message = "上传失败"; Log.Error("WebApi上传图片异常", ex); } string json = r.ToString(); HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(json, Encoding.GetEncoding("UTF-8"), "application/json") }; return(result); }