private void MakeThumbnail(string filePath, string suffix, int width, int height, string mode) { string fileExt = filePath.Substring(filePath.LastIndexOf('.')); string fileHead = filePath.Substring(0, filePath.LastIndexOf('.')); var thumbPath = string.Format("{0}_{1}{2}", fileHead, suffix, fileExt); ; ThumbnailHelper.MakeThumbnail(filePath, thumbPath, width, height, mode, false); }
public async Task <JsonResult> UEditor() { try { string host = string.Format("{0}://{1}", Request.Scheme, Request.Host); List <string> urls = new List <string>(); IFormFile file = Request.Form.Files[0]; Guid fileId = Guid.NewGuid(); DateTime nowDate = DateTime.Now; // 服务器的存储全路径 string destFileName = MapPath(file.FileName); urls.Add(host + file.FileName); // 创建目录路径 if (!Directory.Exists(Path.GetDirectoryName(destFileName))) { Directory.CreateDirectory(Path.GetDirectoryName(destFileName)); } var suffix = Path.GetExtension(file.FileName).ToLower(); string ThumbnailSizes = Request.Form["thumbnailSizes"].FirstOrDefault(); using (var stream = System.IO.File.Create(destFileName)) { await file.CopyToAsync(stream); } // 图片文件扩展名验证正则表达式 Regex regexExtension = new Regex(@".*\.(jpg|jpeg|png|gif|bmp)"); if (regexExtension.IsMatch(destFileName.ToLower())) { string[] ThumbnailSizeArr = new string[] { }; //生成缩略图 if (!string.IsNullOrEmpty(ThumbnailSizes) && (ThumbnailSizeArr = ThumbnailSizes.Split(';')).Length > 0) { for (int i = 0; i < ThumbnailSizeArr.Length; i++) { string size = ThumbnailSizeArr[i]; string ThumbFileName = Path.GetFileNameWithoutExtension(file.FileName) + "_" + size + suffix; string ThumbPath = file.FileName.Replace(Path.GetFileName(file.FileName), ThumbFileName); ThumbnailHelper.MakeThumbnail(Convert.ToInt32(size), MapPath(file.FileName), MapPath(ThumbPath)); urls.Add(host + ThumbPath); } } } //返回所有地址 return(Json(new { Code = 0, Msg = "", Data = urls })); } catch (Exception ex) { LogHelper.SaveLog(ex); return(Json(new { Code = 1, Msg = "服务器异常,请联系管理员!" })); } }
public async Task <JsonResult> Upload() { try { string host = string.Format("{0}://{1}", Request.Scheme, Request.Host); Dictionary <string, string> result = new Dictionary <string, string>(); IFormFile file = Request.Form.Files[0]; // 服务器的存储全路径 string destFileName = MapPath(file.FileName); result.Add("url", host + file.FileName); result.Add("path", host + file.FileName.Substring(0, file.FileName.LastIndexOf("/"))); // 创建目录路径 if (!Directory.Exists(Path.GetDirectoryName(destFileName))) { Directory.CreateDirectory(Path.GetDirectoryName(destFileName)); } var suffix = Path.GetExtension(file.FileName).ToLower(); string ThumbnailSizes = Request.Form["thumbnailSizes"].FirstOrDefault(); using (var stream = System.IO.File.Create(destFileName)) { await file.CopyToAsync(stream); } // 图片文件扩展名验证正则表达式 Regex regexExtension = new Regex(@".*\.(jpg|jpeg|png|gif|bmp)"); if (regexExtension.IsMatch(destFileName.ToLower())) { string[] ThumbnailSizeArr = new string[] { }; //生成缩略图 if (!string.IsNullOrEmpty(ThumbnailSizes) && (ThumbnailSizeArr = ThumbnailSizes.Split(';')).Length > 0) { string[] fileNamesArr = new string[ThumbnailSizeArr.Length]; for (int i = 0; i < ThumbnailSizeArr.Length; i++) { string size = ThumbnailSizeArr[i]; string ThumbFileName = Path.GetFileNameWithoutExtension(file.FileName) + "_" + size + suffix; string ThumbPath = file.FileName.Replace(Path.GetFileName(file.FileName), ThumbFileName); ThumbnailHelper.MakeThumbnail(Convert.ToInt32(size), MapPath(file.FileName), MapPath(ThumbPath)); fileNamesArr[i] = ThumbFileName; } result.Add("names", string.Join("|", fileNamesArr)); } } //返回所有地址 return(Json(new { Code = 0, Msg = "", Data = result })); } catch (Exception ex) { LogHelper.SaveLog(ex); return(Json(new { Code = 1, Msg = "服务器异常,请联系管理员!" })); } }
private void ThumbnailOssPutObject(string bucketName, string imageName, byte[] images, OssClient ossClient) { string graphFileName = string.Format("graph_{0}", imageName); byte[] listGraphSizeByte = ThumbnailHelper.MakeThumbnail(ThumbnailHelper.ReturnPhoto(images), "", 200, 200, "Cut"); var objStream = new MemoryStream(listGraphSizeByte); ossClient.PutObject(config.Bucket, graphFileName, objStream); objStream.Close(); }
public async System.Threading.Tasks.Task <JsonResult> Upload() { try { IFormFile file = Request.Form.Files[0]; Guid fileId = Guid.NewGuid(); DateTime nowDate = DateTime.Now; string path = string.Format("/UploadFiles/{0:yyyyMMdd}/{1}", nowDate, fileId); string url = string.Format("{0}/{1}", path, file.FileName); // 服务器的存储全路径 string destFileName = MapPath(url); // 创建目录路径 if (!Directory.Exists(Path.GetDirectoryName(destFileName))) { Directory.CreateDirectory(Path.GetDirectoryName(destFileName)); } SysAttachment fileData = new SysAttachment(); fileData.Id = fileId; //fileDatas.RelationID = ""; fileData.Name = file.FileName; fileData.Names = ""; fileData.Url = url; fileData.Type = 0; fileData.Suffix = Path.GetExtension(file.FileName).ToLower(); fileData.Path = path; fileData.Status = 0; fileData.Size = 0; fileData.CreateBy = ""; fileData.CreateByName = SSOClient.User.UserName; fileData.CreateTime = nowDate; using (var stream = System.IO.File.Create(destFileName)) { await file.CopyToAsync(stream); } // 图片文件扩展名验证正则表达式 Regex regexExtension = new Regex(@".*\.(jpg|jpeg|png|gif|bmp)"); if (regexExtension.IsMatch(destFileName.ToLower())) { string ThumbnailSizes = Request.Form["thumbnailSizes"].FirstOrDefault(); string[] ThumbnailSizeArr = new string[] { }; //生成缩略图 if (!string.IsNullOrEmpty(ThumbnailSizes) && (ThumbnailSizeArr = ThumbnailSizes.Split(';')).Length > 0) { string[] fileNamesArr = new string[ThumbnailSizeArr.Length]; for (int i = 0; i < ThumbnailSizeArr.Length; i++) { string size = ThumbnailSizeArr[i]; string ThumbFileName = Path.GetFileNameWithoutExtension(url) + "_" + size + fileData.Suffix; string ThumbPath = url.Replace(Path.GetFileName(url), ThumbFileName); ThumbnailHelper.MakeThumbnail(Convert.ToInt32(size), MapPath(url), MapPath(ThumbPath)); fileNamesArr[i] = ThumbFileName; } fileData.Names = string.Join("|", fileNamesArr); } } return(Json(new { Code = 0, Msg = "", Data = fileData })); } catch (Exception ex) { LogHelper.SaveLog(ex); return(Json(new { Code = 1, Msg = "服务器异常,请联系管理员!" })); } }
public async Task <JsonResult> Upload() { try { IFormFile file = Request.Form.Files[0]; Guid fileId = Guid.NewGuid(); DateTime nowDate = DateTime.Now; string path = string.Format("/uploadfiles/{0:yyyy/MMdd}/{1}", nowDate, fileId); string url = string.Format("{0}/{1}", path, file.FileName); // 服务器的存储全路径 string destFileName = MapPath(url); // 创建目录路径 if (!Directory.Exists(Path.GetDirectoryName(destFileName))) { Directory.CreateDirectory(Path.GetDirectoryName(destFileName)); } SysAttachment fileData = new SysAttachment(); fileData.Id = fileId; //fileDatas.RelationID = ""; fileData.Name = file.FileName; fileData.Names = ""; fileData.Url = url; fileData.Type = 0; fileData.Suffix = Path.GetExtension(file.FileName).ToLower(); fileData.Path = path; fileData.Status = 0; fileData.Size = 0; fileData.CreateBy = ""; fileData.CreateByName = SSOClient.User.UserName; fileData.CreateTime = nowDate; string ThumbnailSizes = Request.Form["thumbnailSizes"].FirstOrDefault(); if (string.IsNullOrEmpty(FileServiceUrl)) { //保存本地 using (var stream = System.IO.File.Create(destFileName)) { await file.CopyToAsync(stream); } // 图片文件扩展名验证正则表达式 Regex regexExtension = new Regex(@".*\.(jpg|jpeg|png|gif|bmp)"); if (regexExtension.IsMatch(destFileName.ToLower())) { string[] ThumbnailSizeArr = new string[] { }; //生成缩略图 if (!string.IsNullOrEmpty(ThumbnailSizes) && (ThumbnailSizeArr = ThumbnailSizes.Split(';')).Length > 0) { string[] fileNamesArr = new string[ThumbnailSizeArr.Length]; for (int i = 0; i < ThumbnailSizeArr.Length; i++) { string size = ThumbnailSizeArr[i]; string ThumbFileName = Path.GetFileNameWithoutExtension(url) + "_" + size + fileData.Suffix; string ThumbPath = url.Replace(Path.GetFileName(url), ThumbFileName); ThumbnailHelper.MakeThumbnail(Convert.ToInt32(size), MapPath(url), MapPath(ThumbPath)); fileNamesArr[i] = ThumbFileName; } fileData.Names = string.Join("|", fileNamesArr); } } } else { //保存文件服务器 HttpClient client = new HttpClient(); MultipartFormDataContent form = new MultipartFormDataContent(); byte[] uploadFileBytes = new byte[file.Length]; file.OpenReadStream().Read(uploadFileBytes, 0, (int)file.Length); MemoryStream stream = new MemoryStream(uploadFileBytes); StreamContent fileContent = new StreamContent(stream); fileContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data"); fileContent.Headers.ContentDisposition.FileName = url; form.Add(fileContent); StringContent thumbnailSizes = new StringContent(ThumbnailSizes); thumbnailSizes.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data"); thumbnailSizes.Headers.ContentDisposition.Name = "thumbnailSizes"; form.Add(thumbnailSizes); HttpResponseMessage res = client.PostAsync(FileServiceUrl, form).Result; var json = res.Content.ReadAsStringAsync().Result; JObject result = JObject.Parse(json); fileData.Path = (string)result["data"]["path"]; fileData.Names = (string)result["data"]["names"]; fileData.Url = (string)result["data"]["url"]; } return(Json(new { Code = 0, Msg = "", Data = fileData })); } catch (Exception ex) { LogHelper.SaveLog(ex); return(Json(new { Code = 1, Msg = "服务器异常,请联系管理员!" })); } }