/// <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); }