public JsonResult UploadImage() { var result = ""; if (Request.Files.Count > 0) { var Imgfile = Request.Files[0]; try { var buffer = new byte[Imgfile.ContentLength]; Imgfile.InputStream.Read(buffer, 0, buffer.Length); using (var client = new FileUploadClient()) { var res = client.UploadImage(new ImageUploadRequest() { Contents = buffer, DirectoryName = "BannerConfigImg", MaxHeight = 1920, MaxWidth = 1920 }); if (res.Success && res.Result != null) { result = WebConfigurationManager.AppSettings["DoMain_image"] + res.Result; } } } catch (Exception exp) { WebLog.LogException(exp); } } return(Json(result)); }
public JsonResult UploadImage() { var result = ""; if (Request.Files.Count > 0) { var Imgfile = Request.Files[0]; try { var buffer = new byte[Imgfile.ContentLength]; Imgfile.InputStream.Read(buffer, 0, buffer.Length); using (var client = new FileUploadClient()) { var res = client.UploadImage(new ImageUploadRequest() { Contents = buffer, DirectoryName = "TaskConfig", MaxHeight = 1920, MaxWidth = 1920 }); if (res.Success && res.Result != null) { result = ImageHelper.GetImageUrl(res.Result); } } } catch (Exception exp) { WebLog.LogException(exp); } } return(Json(result)); }
/// <summary>远程保存文件</summary> /// <param name="file">文件</param> /// <param name="filePath">文件路径。访问路径为http://image.tuhu.test+路径</param> /// <param name="maxWidth">图片最大宽,最小值为100。如果不限则为0</param> /// <param name="maxHeight">图片最大高,最小值为100。如果不限则为0</param> /// <returns>1:保存成功;0:没有保存;-1:没有路径;-2:内容为空;-3:maxWidth和maxHeight最少值为100</returns> public static string UploadImage(HttpPostedFileBase file, string filePath, short maxWidth, short maxHeight) { try { if (string.IsNullOrWhiteSpace(filePath)) { return(null); } if (file == null || file.ContentLength < 1) { return(null); } if (maxWidth != 0 && maxHeight != 0 && (maxWidth < 100 || maxHeight < 100)) { return(null); } using (var client = new FileUploadClient()) { var buffer = new byte[file.ContentLength]; file.InputStream.Read(buffer, 0, buffer.Length); var result = client.UploadImage(new ImageUploadRequest(filePath, buffer, maxWidth, maxHeight)); result.ThrowIfException(true); return(result.Result); } } catch (Exception ex) { WebLog.LogException(ex); } return(null); }
public static string ImageUploadFile(string pathFormat, byte[] uploadFileBytes, string uploadFileName, short maxWidthHeight) { var data = string.Empty; try { using (var client = new FileUploadClient()) { var result = client.UploadImage(new ImageUploadRequest(pathFormat, uploadFileBytes)); if (result.Success) { data = result.Result; } result.ThrowIfException(true); } } catch (Exception e) { //Result.State = UploadState.FileAccessError; //Result.ErrorMessage = e.Message; } finally { //WriteResult(); } return(data); }
public ActionResult ImageUploadToAli(string filepath, int maxwidth = 0, int maxheight = 0) { if (Request.Files.Count > 0 && !String.IsNullOrWhiteSpace(filepath)) { var Imgfile = Request.Files[0]; var buffer = new byte[Imgfile.ContentLength]; Imgfile.InputStream.Read(buffer, 0, buffer.Length); filepath = filepath + BitConverter.ToString(HashAlgorithm.Create("MD5").ComputeHash(buffer)).Replace("-", "").ToString() + Path.GetExtension(Imgfile.FileName); using (var client = new FileUploadClient()) { client.UploadImage(filepath, buffer, Math.Max(maxwidth, 0), Math.Max(maxheight, 0)); } return(Content(filepath.ToString())); } return(Content(null)); }