public async Task <JsonResult> UploadServiceCodeTypeConfigLogo() { if (SingleFile == null) { return(Json(Tuple.Create(false, "请选择上传的图片!"))); } try { var bytes = new byte[SingleFile.InputStream.Length]; SingleFile.InputStream.Read(bytes, 0, bytes.Length); using (var client = new FileUploadClient()) { var result = await client.UploadImageAsync(new Service.Utility.Request.ImageUploadRequest { Contents = bytes, DirectoryName = "ServiceCodeTypeConfig" }); result.ThrowIfException(true); if (result.Success) { return(Json(Tuple.Create(true, result.Result.GetImageUrl()))); } return(Json(Tuple.Create(false, $"上传失败:{result.ErrorMessage}"))); } } catch (Exception ex) { return(Json(Tuple.Create(false, $"服务端异常:{ex.Message}"))); } }
public async Task <JsonResult> UploadBeautyBannerImage(string type) { if (SingleFile == null) { return(Json(Tuple.Create(false, "请选择上传的图片!"))); } try { var bytes = new byte[SingleFile.InputStream.Length]; SingleFile.InputStream.Read(bytes, 0, bytes.Length); using (var client = new FileUploadClient()) { var result = await client.UploadImageAsync(new Service.Utility.Request.ImageUploadRequest { Contents = bytes, DirectoryName = "BeautyHomePageBanner" }); result.ThrowIfException(true); if (result.Success) { using (var image = System.Drawing.Image.FromStream(SingleFile.InputStream)) { if (image.Width * 1.0 / image.Height != 2.0 / 1) { return(Json(Tuple.Create(false, "图片宽高尺寸比例应为(2:1)"))); } } return(Json(Tuple.Create(true, result.Result.GetImageUrl(), type))); } return(Json(Tuple.Create(false, $"上传失败:{result.ErrorMessage}", type))); } } catch (Exception ex) { return(Json(Tuple.Create(false, $"服务端异常:{ex.Message}", type))); } }
/// <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 async Task <string> UploadImageAsync(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 = await client.UploadImageAsync(new ImageUploadRequest(filePath, buffer, maxWidth, maxHeight)); result.ThrowIfException(true); return(result.Result); } } catch (Exception ex) { WebLog.LogException(ex); } return(null); }