/// <summary> /// 存储图片 根据临时路径 /// </summary> /// <param name="type">类型</param> /// <param name="tempUrl">图片临时路径 /fafafaf/afa/faf.jpg</param> /// <param name="mapPath">服务器虚拟路径 d:\wi\web</param> /// <param name="domainName">http://+域名</param> /// <param name="rootPath">根路径 upload</param> /// <param name="tempPath">临时文件根路径 tempHSCP</param> /// <returns></returns> public static string BaseSave(ImagePathType type, string tempUrl) { //判断是不是临时图片 if (string.IsNullOrWhiteSpace(tempUrl)) { return(""); } var contentRoot = Directory.GetCurrentDirectory(); var webRoot = Path.Combine(contentRoot, "wwwroot"); FileInfo file = new FileInfo(tempUrl); // var parsedContentDisposition = ContentDispositionHeaderValue.Parse(tempUrl); var originalName = file.Name.Replace("\"", ""); // var ext = Path.GetExtension(Path.Combine(webRoot, originalName)); // 获取 固定路径 string fixedPath = GetPathByType(type).FixedPath; // 获取 存储图片宽高,格式为:[宽x高,宽x高,宽x高...]注:宽高中间用小写"x"隔开 var sizeArray = tempUrl.Substring(tempUrl.LastIndexOf("[") + 1, tempUrl.IndexOf("]") - tempUrl.LastIndexOf("[") - 1).Split('-'); var sizeAll = tempUrl.Substring(tempUrl.LastIndexOf("["), tempUrl.IndexOf("]") - tempUrl.LastIndexOf("[") + 1); var tempUrlNoExt = tempUrl.Substring(0, tempUrl.LastIndexOf(".")); var ext = Path.GetExtension(tempUrl);// tempUrl.Substring(tempUrl.LastIndexOf(".")); string savePathData = ""; for (int i = 0; i < sizeArray.Length; i++) { //获取一条宽高 var wh = sizeArray[i].Split('x'); if (wh.Length == 2) { //宽 int width = int.Parse(wh[0]); //高 int height = int.Parse(wh[1]); var fileName = Path.Combine("upload" + fixedPath, Guid.NewGuid().ToString() + "_" + width + "_" + height + ext); savePathData += "\\" + fileName + ";"; //图片处理 IMGUtility.Thumbnail(tempUrl, Path.Combine(webRoot, fileName), width, height, CutMode.WH); } } if (!string.IsNullOrWhiteSpace(savePathData)) { savePathData = savePathData.Substring(0, savePathData.Length - 1); } return(savePathData); }
/// <summary> /// 存储图片 根据文件流 /// </summary> /// <param name="type">类型</param> /// <param name="s">图片临时路径</param> /// <param name="mapPath">绝对路径(D:\相对路径)</param> /// <param name="domainName">http://+域名</param> /// <param name="rootPath">根路径</param> /// <returns></returns> public static string BaseSave(ImagePathType type, Stream s, string mapPath, string domainName) { // 获取 当前所有路径 var paths = GetPathByTypes(type); // 保存图片或的路径,多个路径中间用逗号","隔开 string savePathData = ""; foreach (var p in paths) { //宽 int width = p.Width; //高 int height = p.Height; //固定路径 string fixedPath = p.FixedPath.Trim('/'); var dt = DateTime.Now; var random = new Random().Next(100, 999); string fn = "/" + dt.ToString("HHmmssff") + random + "_" + width + "x" + height + ".jpg"; string path = fixedPath + "/" + dt.ToString("yyyy") + "/" + dt.ToString("MM") + "/" + dt.ToString("dd"); //图片保存相对路径 var relativePath = path + fn; //图片保存路径 例:"D:\"+相对路径 var savePath = Path.Combine(mapPath, relativePath); //图片保存路径数据 http://+域名+相对路径 var saveData = domainName + "/" + relativePath; if (savePathData != "") { savePathData = savePathData + "," + saveData; } else { savePathData = saveData; } //图片处理 IMGUtility.Thumbnail(s, savePath, width, height, CutMode.WH); } return(savePathData); }
// ---------------------------------------------------------------------- /// <summary> /// SmallImageUri Constructor /// </summary> /// <param name="bigFileName">The filename and path of the big file</param> /// <param name="smallFileIndicator">The filename addon which indicates a small file</param> /// <param name="relativePath">The relative path to insert between the filename/path and the root path</param> public SmallImageUri(string bigFileName, string smallFileIndicator, string relativePath) { _BigFileName = bigFileName; _RelativePath = relativePath; _SmallFileIndicator = smallFileIndicator; bool isRelative = !bigFileName.Contains("http://"); bigFileName = RemoveLeadingSlash(_BigFileName); string urlRewritePath = HttpUtility.UrlDecode(bigFileName); // Check if using image.axd if (bigFileName.Contains(_ImageServer)) { string[] att = bigFileName.Split('?'); if (att.Length == 2) { string tempFile = att[1]; att = tempFile.Split('&'); foreach (string s in att) { string[] variable = s.Split('='); if (variable.Length == 2 && variable[0] == _ImageServerPicturePathVar) { _RequestedFileName = RemoveLeadingSlash(HttpUtility.UrlDecode(variable[1])); _PathType = ImagePathType.ImageAxd; _FoundSmallFile = true; break; } } } } // Maybe we're using BE.NET url-rewriting else if (urlRewritePath.IndexOf(urlRewritePath) != -1 && urlRewritePath.Length > _UrlRewriteExtension.Length && String.Compare(urlRewritePath.Substring(urlRewritePath.Length - _UrlRewriteExtension.Length, _UrlRewriteExtension.Length), _UrlRewriteExtension, true) == 0) { int start = urlRewritePath.IndexOf(_UrlRewriteDirectory) + _UrlRewriteDirectory.Length; string relevantPath = urlRewritePath.Substring(start); relevantPath = relevantPath.Remove(relevantPath.Length - 5); _RequestedFileName = RemoveLeadingSlash(relevantPath); _PathType = ImagePathType.UrlRewrite; _FoundSmallFile = true; } // Absolute Image Path (No file handler) else if (!isRelative) { _PathType = ImagePathType.Absolute; } // Relative Image Path (No file handler) else { _PathType = ImagePathType.Relative; } if (_FoundSmallFile) { // Check to see if the file is already small string fileCheck = Path.GetFileNameWithoutExtension(_RequestedFileName); if (fileCheck.Length > _SmallFileIndicator.Length && fileCheck.Substring(fileCheck.Length - _SmallFileIndicator.Length) == _SmallFileIndicator) { _FoundSmallFile = false; _BigIsSmall = true; } } }
/// <summary> /// 根据图片类型获取固定路径 /// </summary> /// <param name="type">类型</param> /// <returns></returns> private static List <ImagePath> GetPathByTypes(ImagePathType type) { return(imagePathData.FindAll(o => o.Type == type)); }
/// <summary> /// 根据图片类型获取固定路径 /// </summary> /// <param name="type">类型</param> /// <returns></returns> private static ImagePath GetPathByType(ImagePathType type) { return(imagePathData.Find(o => o.Type == type)); }
/// <summary> /// 存储图片 根据临时路径 /// </summary> /// <param name="type">类型</param> /// <param name="tempUrl">图片临时路径 /fafafaf/afa/faf.jpg</param> /// <param name="mapPath">服务器虚拟路径 d:\wi\web</param> /// <param name="domainName">http://+域名</param> /// <param name="rootPath">根路径 upload</param> /// <param name="tempPath">临时文件根路径 tempConan</param> /// <returns></returns> public static string BaseSave(ImagePathType type, string tempUrl, string mapPath, string domainName, string rootPath, string tempPath = "tempConan") { //判断是不是临时图片 if (string.IsNullOrEmpty(tempUrl) || tempUrl.IndexOf(tempPath) == -1) { return(tempUrl); } // 保存图片或的路径,多个路径中间用逗号","隔开 string savePathData = ""; // 去掉临时图片路径的开头"/" if (tempUrl.StartsWith("/")) { tempUrl = tempUrl.Substring(1); } // 获取 固定路径 string fixedPath = GetPathByType(type).FixedPath; // 获取 存储图片宽高,格式为:[宽x高,宽x高,宽x高...]注:宽高中间用小写"x"隔开 var sizeArray = tempUrl.Substring(tempUrl.LastIndexOf("[") + 1, tempUrl.IndexOf("]") - tempUrl.LastIndexOf("[") - 1).Split('-'); var sizeAll = tempUrl.Substring(tempUrl.LastIndexOf("["), tempUrl.IndexOf("]") - tempUrl.LastIndexOf("[") + 1); var tempUrlNoExt = tempUrl.Substring(0, tempUrl.LastIndexOf(".")); var ext = Path.GetExtension(tempUrl);// tempUrl.Substring(tempUrl.LastIndexOf(".")); for (int i = 0; i < sizeArray.Length; i++) { //获取一条宽高 var wh = sizeArray[i].Split('x'); if (wh.Length == 2) { //宽 int width = int.Parse(wh[0]); //高 int height = int.Parse(wh[1]); //图片保存相对路径 string tpath = tempUrlNoExt.Replace(sizeAll, "").Replace(tempPath, "").Replace("_", "").Trim(new char[] { '/', '\\' }); var relativePath = $"{fixedPath}/{tpath}/{sizeArray[i]}{ext}".TrimStart('/');// tempUrlNoExt.Replace(sizeAll, "").Replace(tempPath, rootPath + fixedPath) + sizeArray[i] + ext; //图片保存路径 例:"D:\"+相对路径 var savePath = Path.Combine(rootPath, relativePath.Replace("/", "\\")); //图片保存路径数据 http://+域名+相对路径 string saveData; if (relativePath.StartsWith("/")) { saveData = $"{domainName}{relativePath}"; } else { saveData = $"{domainName}/{relativePath}"; } if (savePathData != "") { savePathData = savePathData + "," + saveData; } else { savePathData = saveData; } //图片处理 IMGUtility.Thumbnail(Path.Combine(mapPath, tempUrl), savePath, width, height, IMGUtility.CutMode.WH); } } return(savePathData); }
/// <summary> /// 根据图片类型获取固定路径 /// </summary> /// <param name="type">类型</param> /// <returns></returns> private static List <ImagePath> GetPathByTypes(ImagePathType type) { return(imagePathData.Where(o => o.Type == type).ToList()); }
/// <summary> /// 根据图片类型获取固定路径 /// </summary> /// <param name="type">类型</param> /// <returns></returns> private static ImagePath GetPathByType(ImagePathType type) { return(imagePathData.FirstOrDefault(o => o.Type == type)); }