/// <summary> /// 添加或者修改Product的方法 /// </summary> /// <param name="input"></param> /// <returns></returns> public async Task <ProductListDto> CreateOrUpdateProductDto(ProductEditDto input) { string webRootPath = _hostingEnvironment.WebRootPath; if (!string.IsNullOrEmpty(input.Img64) && !string.IsNullOrEmpty(input.FileName)) { var base64 = new WechatImgBase64() { fileName = input.FileName, imageBase64 = input.Img64 }; var photoUrl = await FilesPostsBase64(base64, "product"); input.PhotoUrl = !string.IsNullOrEmpty(photoUrl) ? photoUrl : input.PhotoUrl; } if (input.Id.HasValue) { var entity = _productRepository.GetAsync(input.Id.MapTo <Guid>()).Result; var url = entity.PhotoUrl; var result = await UpdateProductAsync(input); //删除原来的单个图片 if (url != result.PhotoUrl && url != "/assets/img/default.png") { if (File.Exists(webRootPath + url)) { File.Delete(webRootPath + url); } } return(result); } else { return(await CreateProductAsync(input)); } }
/// <summary> /// base64转换 /// </summary> /// <param name="input"></param> /// <returns></returns> public async Task <string> FilesPostsBase64(WechatImgBase64 input, string fileName) { var saveUrl = ""; if (!string.IsNullOrWhiteSpace(input.imageBase64)) { var reg = new Regex("data:image/(.*);base64,"); input.imageBase64 = reg.Replace(input.imageBase64, ""); byte[] imageByte = Convert.FromBase64String(input.imageBase64); var memorystream = new MemoryStream(imageByte); string webRootPath = _hostingEnvironment.WebRootPath; string contentRootPath = _hostingEnvironment.ContentRootPath; string fileExt = Path.GetExtension(input.fileName); //文件扩展名,不含“.” string newFileName = Guid.NewGuid().ToString() + fileExt; //随机生成新的文件名 var fileDire = webRootPath + string.Format("/upload/{0}/", fileName); if (!Directory.Exists(fileDire)) { Directory.CreateDirectory(fileDire); } var filePath = fileDire + newFileName; using (var stream = new FileStream(filePath, FileMode.Create)) { await memorystream.CopyToAsync(stream); } saveUrl = filePath.Substring(webRootPath.Length); return(saveUrl); } return(saveUrl); }
//public async Task<IActionResult> FilesPostsBase64([FromBody]WechatImgBase64 input) public Task <IActionResult> FilesPostsBase64([FromBody] WechatImgBase64 input) { if (!string.IsNullOrWhiteSpace(input.imageBase64)) { var reg = new Regex("data:image/(.*);base64,"); input.imageBase64 = reg.Replace(input.imageBase64, ""); byte[] imageByte = Convert.FromBase64String(input.imageBase64); //var memorystream = new MemoryStream(imageByte); string webRootPath = _hostingEnvironment.WebRootPath; string contentRootPath = _hostingEnvironment.ContentRootPath; string fileExt = Path.GetExtension(input.fileName); //文件扩展名,不含“.” string newFileName = Guid.NewGuid().ToString() + fileExt; //随机生成新的文件名 var fileDire = webRootPath + "/upload/shop/"; if (!Directory.Exists(fileDire)) { Directory.CreateDirectory(fileDire); } var filePath = fileDire + newFileName; //2018-7-6 压缩后保存 using (Image <Rgba32> image = SixLabors.ImageSharp.Image.Load(imageByte)) { //如果高度大于200 就需要压缩 if (image.Height > 200) { var width = (int)((200 / image.Height) * image.Width); image.Mutate(x => x.Resize(width, 200)); } image.Save(filePath); } //using (var stream = new FileStream(filePath, FileMode.Create)) //{ // await memorystream.CopyToAsync(stream); //} var saveUrl = filePath.Substring(webRootPath.Length); return(Task.FromResult((IActionResult)Json(new APIResultDto() { Code = 0, Msg = "上传数据成功", Data = saveUrl }))); } return(Task.FromResult((IActionResult)Json(new APIResultDto() { Code = 901, Msg = "上传数据不能为空" }))); }