public async Task <IActionResult> UploadImgage(ImgPathEnum ImgPathEnum, bool IsFullPath = false) { var multipartBoundary = Request.GetMultipartBoundary(); if (string.IsNullOrEmpty(multipartBoundary)) { return(BadRequest($"Expected a multipart request, but got '{Request.ContentType}'.")); } // Used to accumulate all the form url encoded key value pairs in the request. var formAccumulator = new KeyValueAccumulator(); string targetFilePath = null; var reader = new MultipartReader(multipartBoundary, HttpContext.Request.Body); var section = await reader.ReadNextSectionAsync(); while (section != null) { // This will reparse the content disposition header // Create a FileMultipartSection using it's constructor to pass // in a cached disposition header var fileSection = section.AsFileSection(); if (fileSection != null) { var fileName = fileSection.FileName; targetFilePath = Path.Combine(_hostingEnvironment.ContentRootPath, Guid.NewGuid().ToString()); using (var targetStream = System.IO.File.Create(targetFilePath)) { await fileSection.FileStream.CopyToAsync(targetStream); _logger.LogInformation($"Copied the uploaded file '{fileName}' to '{targetFilePath}'."); } } else { var formSection = section.AsFormDataSection(); if (formSection != null) { var name = formSection.Name; var value = await formSection.GetValueAsync(); formAccumulator.Append(name, value); if (formAccumulator.ValueCount > FormReader.DefaultValueCountLimit) { throw new InvalidDataException( $"Form key count limit {FormReader.DefaultValueCountLimit} exceeded."); } } } // Drains any remaining section body that has not been consumed and // reads the headers for the next section. section = await reader.ReadNextSectionAsync(); } return(Json(targetFilePath)); }
/// <summary> /// 上传图像 /// </summary> /// <param name="HttpContext">上下文</param> /// <param name="Path">路径枚举</param> /// <param name="IsFullPath">是否返回全路径</param> public static string UploadImgageReturnPath(HttpContext HttpContext, ImgPathEnum Path, bool IsFullPath) { System.IO.Stream userfile = HttpContext.Request.Body; string FileName = DateTime.Now.ToString("yyyyMMddHHmmssfff"); string FileExt = HttpContext.Request.Headers["fileext"]; if (userfile == null) { return("File Upload error (文件上传错误!)"); } if (string.IsNullOrEmpty(FileExt)) { FileExt = "png"; } FileExt = FileExt.ToLower(); string[] extArr = new string[] { "gif", "jpg", "jpeg", "png", "ico" }; if (string.IsNullOrEmpty(FileExt) || !extArr.Contains(FileExt)) { return("File format error (文件格式错误!)"); } string uploadfolder = string.Format("{0}\\img\\{1}\\", ConstDefine.ImgSavePath, (int)Path); string uploadfileurl = ConstDefine.ImgSiteDomain; if (!Directory.Exists(uploadfolder)) { Directory.CreateDirectory(uploadfolder); } FileExt = "." + FileExt; byte[] buffer = new byte[256]; int len; string FileFullPath = uploadfolder + FileName + FileExt; FileStream fs = new FileStream(FileFullPath, FileMode.Create, FileAccess.Write); while ((len = userfile.Read(buffer, 0, buffer.Length)) > 0) { fs.Write(buffer, 0, len); } fs.Dispose(); string imgUrl = string.Empty; if (IsFullPath) { imgUrl = uploadfileurl + "/" + (int)Path + "/" + FileName + FileExt; } else { imgUrl = FileName + FileExt; } try { MakeThumbnail(FileFullPath, FileFullPath + "_small.jpeg", 150, 80); } catch (Exception ex) { return(imgUrl); } return(imgUrl); }
/// <summary> /// 获取图片完整访问地址 /// </summary> /// <param name="url">图片名称</param> /// <param name="Path">路径枚举</param> /// <param name="IsSmall">是否获取压缩小图</param> /// <returns></returns> public static string GetImgFullUrl(string url, ImgPathEnum Path, bool IsSmall = true) { if (IsSmall) { return(string.Format("{0}/{1}/{2}{3}", ConstDefine.ImgSiteDomain, (int)Path, url, "_small.jpeg")); } else { return(string.Format("{0}/{1}/{2}", ConstDefine.ImgSiteDomain, (int)Path, url)); } }
public JsonResult UploadEditImgage(ImgPathEnum ImgPathEnum, bool IsFullPath = false) { string url = Vic.Core.Utils.Common.UploadImgageReturnPath(this.HttpContext, ImgPathEnum, IsFullPath); return(Json(new { err = "", msg = url })); }
public ContentResult UploadImgageNew(ImgPathEnum ImgPathEnum, bool IsFullPath = false) { string url = Vic.Core.Utils.Common.UploadImgageReturnPath(this.HttpContext, ImgPathEnum, IsFullPath); return(Content(url)); }