public string fileSaveAs(HttpPostedFile postedFile, bool isThumbnail, bool isWater) { try { string fileExt = Utils.GetFileExt(postedFile.FileName); int contentLength = postedFile.ContentLength; string str1 = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf("\\") + 1); string str2 = Utils.GetRamCode() + "." + fileExt; string str3 = "thumb_" + str2; string upLoadPath = this.GetUpLoadPath(); string mapPath = Utils.GetMapPath(upLoadPath); string str4 = upLoadPath + str2; string str5 = upLoadPath + str3; if (!this.CheckFileExt(fileExt)) { return("{\"status\": 0, \"msg\": \"不允许上传" + fileExt + "类型的文件!\"}"); } if (!this.CheckFileSize(fileExt, contentLength)) { return("{\"status\": 0, \"msg\": \"文件超过限制的大小!\"}"); } if (!Directory.Exists(mapPath)) { Directory.CreateDirectory(mapPath); } postedFile.SaveAs(mapPath + str2); if (this.IsImage(fileExt) && (this.siteConfig.imgmaxheight > 0 || this.siteConfig.imgmaxwidth > 0)) { Thumbnail.MakeThumbnailImage(mapPath + str2, mapPath + str2, this.siteConfig.imgmaxwidth, this.siteConfig.imgmaxheight); } if (this.IsImage(fileExt) && isThumbnail && this.siteConfig.thumbnailwidth > 0 && this.siteConfig.thumbnailheight > 0) { Thumbnail.MakeThumbnailImage(mapPath + str2, mapPath + str3, this.siteConfig.thumbnailwidth, this.siteConfig.thumbnailheight, "Cut"); } else { str5 = str4; } if (this.IsWaterMark(fileExt) && isWater) { switch (this.siteConfig.watermarktype) { case 1: WaterMark.AddImageSignText(str4, str4, this.siteConfig.watermarktext, this.siteConfig.watermarkposition, this.siteConfig.watermarkimgquality, this.siteConfig.watermarkfont, this.siteConfig.watermarkfontsize); break; case 2: WaterMark.AddImageSignPic(str4, str4, this.siteConfig.watermarkpic, this.siteConfig.watermarkposition, this.siteConfig.watermarkimgquality, this.siteConfig.watermarktransparency); break; } } return("{\"status\": 1, \"msg\": \"上传文件成功!\", \"name\": \"" + str1 + "\", \"path\": \"" + str4 + "\", \"thumb\": \"" + str5 + "\", \"size\": " + (object)contentLength + ", \"ext\": \"" + fileExt + "\"}"); } catch { return("{\"status\": 0, \"msg\": \"上传过程中发生意外错误!\"}"); } }
/// <summary> /// 保存远程文件到本地 /// </summary> /// <param name="fileUri">URI地址</param> /// <returns>上传后的路径</returns> public string remoteSaveAs(string fileUri) { WebClient client = new WebClient(); string fileExt = string.Empty; //文件扩展名,不含“.” if (fileUri.LastIndexOf(".") == -1) { fileExt = "gif"; } else { fileExt = Utils.GetFileExt(fileUri); } string newFileName = Utils.GetRamCode() + "." + fileExt; //随机生成新的文件名 string upLoadPath = GetUpLoadPath(); //上传目录相对路径 string fullUpLoadPath = Utils.GetMapPath(upLoadPath); //上传目录的物理路径 string newFilePath = upLoadPath + newFileName; //上传后的路径 Model.files modelNewFile = new Model.files(); //新文件所需实体类 //检查上传的物理路径是否存在,不存在则创建 if (!Directory.Exists(fullUpLoadPath)) { Directory.CreateDirectory(fullUpLoadPath); } try { client.DownloadFile(fileUri, fullUpLoadPath + newFileName); //如果是图片,检查是否需要打水印 if (IsWaterMark(fileExt)) { switch (this.siteConfig.watermarktype) { case 1: WaterMark.AddImageSignText(newFilePath, newFilePath, this.siteConfig.watermarktext, this.siteConfig.watermarkposition, this.siteConfig.watermarkimgquality, this.siteConfig.watermarkfont, this.siteConfig.watermarkfontsize); break; case 2: WaterMark.AddImageSignPic(newFilePath, newFilePath, this.siteConfig.watermarkpic, this.siteConfig.watermarkposition, this.siteConfig.watermarkimgquality, this.siteConfig.watermarktransparency); break; } } //存储文件信息 modelNewFile = new BLL.files().Add(newFilePath, userID); } catch { return(string.Empty); } client.Dispose(); return(Utils.GetAppSettings("FileUrl") + modelNewFile.file_md5); }
/// <summary> /// 儲存遠端檔到本地 /// </summary> /// <param name="fileUri">URI地址</param> /// <returns>上傳後的路徑</returns> public string remoteSaveAs(string fileUri) { WebClient client = new WebClient(); string fileExt = string.Empty; //文件副檔名,不含“.” if (fileUri.LastIndexOf(".") == -1) { fileExt = "gif"; } else { fileExt = Utils.GetFileExt(fileUri); } string newFileName = Utils.GetRamCode() + "." + fileExt; //隨機生成新的檔案名 string upLoadPath = GetUpLoadPath(); //上傳目錄相對路徑 string fullUpLoadPath = Utils.GetMapPath(upLoadPath); //上傳目錄的物理路徑 string newFilePath = upLoadPath + newFileName; //上傳後的路徑 //檢查上傳的物理路徑是否存在,不存在則創建 if (!Directory.Exists(fullUpLoadPath)) { Directory.CreateDirectory(fullUpLoadPath); } try { client.DownloadFile(fileUri, fullUpLoadPath + newFileName); //如果是圖片,檢查是否需要打浮水印 if (IsWaterMark(fileExt)) { switch (this.siteConfig.watermarktype) { case 1: WaterMark.AddImageSignText(newFilePath, newFilePath, this.siteConfig.watermarktext, this.siteConfig.watermarkposition, this.siteConfig.watermarkimgquality, this.siteConfig.watermarkfont, this.siteConfig.watermarkfontsize); break; case 2: WaterMark.AddImageSignPic(newFilePath, newFilePath, this.siteConfig.watermarkpic, this.siteConfig.watermarkposition, this.siteConfig.watermarkimgquality, this.siteConfig.watermarktransparency); break; } } } catch { return(string.Empty); } client.Dispose(); return(newFilePath); }
public virtual IActionResult ImageWaterMarkLogo(string sf, string u, string l, int s, int q, int t) { if (sf.IsNotSet()) { sf = ("/WaterMark/" + u).Replace("//", "/"); } var newPath = MapWebRootPath(sf); var path = MapWebRootPath(u); var logoPath = MapWebRootPath(l); var bytes = WaterMark.AddImageSignPic(path, logoPath, s, q, t); Directory.CreateDirectory(Path.GetDirectoryName(newPath)); System.IO.File.WriteAllBytes(newPath, bytes); return(Success(sf)); }
public string remoteSaveAs(string fileUri) { WebClient webClient = new WebClient(); string empty = string.Empty; string _fileExt = fileUri.LastIndexOf(".") != -1 ? Utils.GetFileExt(fileUri) : "gif"; string str1 = Utils.GetRamCode() + "." + _fileExt; string upLoadPath = this.GetUpLoadPath(); string mapPath = Utils.GetMapPath(upLoadPath); string str2 = upLoadPath + str1; if (!Directory.Exists(mapPath)) { Directory.CreateDirectory(mapPath); } try { webClient.DownloadFile(fileUri, mapPath + str1); if (this.IsWaterMark(_fileExt)) { switch (this.siteConfig.watermarktype) { case 1: WaterMark.AddImageSignText(str2, str2, this.siteConfig.watermarktext, this.siteConfig.watermarkposition, this.siteConfig.watermarkimgquality, this.siteConfig.watermarkfont, this.siteConfig.watermarkfontsize); break; case 2: WaterMark.AddImageSignPic(str2, str2, this.siteConfig.watermarkpic, this.siteConfig.watermarkposition, this.siteConfig.watermarkimgquality, this.siteConfig.watermarktransparency); break; } } } catch { return(string.Empty); } webClient.Dispose(); return(str2); }
public override void Process() { byte[] uploadFileBytes = null; string uploadFileName = null; if (UploadConfig.Base64) { uploadFileName = UploadConfig.Base64Filename; uploadFileBytes = Convert.FromBase64String(Request[UploadConfig.UploadFieldName]); } else { var file = Request.Files[UploadConfig.UploadFieldName]; uploadFileName = file.FileName; if (!CheckFileType(uploadFileName)) { Result.State = UploadState.TypeNotAllow; WriteResult(); return; } if (!CheckFileSize(file.ContentLength)) { Result.State = UploadState.SizeLimitExceed; WriteResult(); return; } uploadFileBytes = new byte[file.ContentLength]; try { file.InputStream.Read(uploadFileBytes, 0, file.ContentLength); } catch (Exception) { Result.State = UploadState.NetworkError; WriteResult(); } } Result.OriginFileName = uploadFileName; var savePath = PathFormatter.Format(uploadFileName, UploadConfig.PathFormat); var localPath = Server.MapPath(savePath); //try //{ // if (!Directory.Exists(Path.GetDirectoryName(localPath))) // { // Directory.CreateDirectory(Path.GetDirectoryName(localPath)); // } // File.WriteAllBytes(localPath, uploadFileBytes); // Result.Url = savePath; // Result.State = UploadState.Success; //} try { if (!Directory.Exists(Path.GetDirectoryName(localPath))) { Directory.CreateDirectory(Path.GetDirectoryName(localPath)); } File.WriteAllBytes(localPath, uploadFileBytes); WaterMark.AddImageSignText(localPath, localPath, "Akic", 9, 80, "Tahoma", 12); WaterMark.AddImageSignPic(localPath, localPath, @"Content/css/images/Akic_Logo.png", 9, 80, 5); Result.Url = savePath; Result.State = UploadState.Success; } catch (Exception e) { Result.State = UploadState.FileAccessError; Result.ErrorMessage = e.Message; } finally { WriteResult(); } }
/// <summary> /// 通过文件流上传文件方法 /// </summary> /// <param name="byteData">文件字节数组</param> /// <param name="fileName">文件名</param> /// <param name="isThumbnail">是否生成缩略图</param> /// <param name="isWater">是否打水印</param> /// <returns>上传成功返回JSON字符串</returns> public string FileSaveAs(byte[] byteData, string fileName, bool isThumbnail, bool isWater) { try { string fileExt = Path.GetExtension(fileName).Trim('.'); //文件扩展名,不含“.” string newFileName = Utils.GetRamCode() + "." + fileExt; //随机生成新的文件名 string newThumbnailFileName = "thumb_" + newFileName; //随机生成缩略图文件名 string upLoadPath = GetUpLoadPath(); //本地上传目录相对路径 string fullUpLoadPath = Utils.GetMapPath(upLoadPath); //本地上传目录的物理路径 string newFilePath = upLoadPath + newFileName; //本地上传后的路径 string newThumbnailPath = upLoadPath + newThumbnailFileName; //本地上传后的缩略图路径 byte[] thumbData = null; //缩略图文件流 //检查文件字节数组是否为NULL if (byteData == null) { return("{\"status\": 0, \"msg\": \"请选择要上传的文件!\"}"); } //检查文件扩展名是否合法 if (!CheckFileExt(fileExt)) { return("{\"status\": 0, \"msg\": \"不允许上传" + fileExt + "类型的文件!\"}"); } //检查文件大小是否合法 if (!CheckFileSize(fileExt, byteData.Length)) { return("{\"status\": 0, \"msg\": \"文件超过限制的大小!\"}"); } //如果是图片,检查图片是否超出最大尺寸,是则裁剪 if (IsImage(fileExt) && (this.sysConfig.imgmaxheight > 0 || this.sysConfig.imgmaxwidth > 0)) { byteData = Thumbnail.MakeThumbnailImage(byteData, fileExt, this.sysConfig.imgmaxwidth, this.sysConfig.imgmaxheight); } //如果是图片,检查是否需要生成缩略图,是则生成 if (IsImage(fileExt) && isThumbnail && this.sysConfig.thumbnailwidth > 0 && this.sysConfig.thumbnailheight > 0) { thumbData = Thumbnail.MakeThumbnailImage(byteData, fileExt, this.sysConfig.thumbnailwidth, this.sysConfig.thumbnailheight, this.sysConfig.thumbnailmode); } else { newThumbnailPath = newFilePath; //不生成缩略图则返回原图 } //如果是图片,检查是否需要打水印 if (IsWaterMark(fileExt) && isWater) { switch (this.sysConfig.watermarktype) { case 1: byteData = WaterMark.AddImageSignText(byteData, fileExt, this.sysConfig.watermarktext, this.sysConfig.watermarkposition, this.sysConfig.watermarkimgquality, this.sysConfig.watermarkfont, this.sysConfig.watermarkfontsize); break; case 2: byteData = WaterMark.AddImageSignPic(byteData, fileExt, this.sysConfig.watermarkpic, this.sysConfig.watermarkposition, this.sysConfig.watermarkimgquality, this.sysConfig.watermarktransparency); break; } } //检查本地上传的物理路径是否存在,不存在则创建 if (!Directory.Exists(fullUpLoadPath)) { Directory.CreateDirectory(fullUpLoadPath); } //保存主文件 FileHelper.SaveFile(byteData, fullUpLoadPath + newFileName); //保存缩略图文件 if (thumbData != null) { FileHelper.SaveFile(thumbData, fullUpLoadPath + newThumbnailFileName); } //处理完毕,返回JOSN格式的文件信息 return("{\"status\": 1, \"msg\": \"上传文件成功!\", \"name\": \"" + fileName + "\", \"path\": \"" + newFilePath + "\", \"thumb\": \"" + newThumbnailPath + "\", \"size\": " + byteData.Length + ", \"ext\": \"" + fileExt + "\"}"); } catch { return("{\"status\": 0, \"msg\": \"上传过程中发生意外错误!\"}"); } }
/// <summary> /// 檔上傳方法 /// </summary> /// <param name="postedFile">文件流</param> /// <param name="isThumbnail">是否生成縮略圖</param> /// <param name="isWater">是否加浮水印</param> /// <returns>上傳後檔案資訊</returns> public string fileSaveAs(HttpPostedFile postedFile, bool isThumbnail, bool isWater) { try { string fileExt = Utils.GetFileExt(postedFile.FileName); //文件副檔名,不含“.” int fileSize = postedFile.ContentLength; //獲得檔案大小,以位元組為單位 string fileName = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf(@"\") + 1); //取得原檔案名 string newFileName = Utils.GetRamCode() + "." + fileExt; //隨機生成新的檔案名 string newThumbnailFileName = "thumb_" + newFileName; //隨機生成縮略圖檔案名 string upLoadPath = GetUpLoadPath(); //上傳目錄相對路徑 string fullUpLoadPath = Utils.GetMapPath(upLoadPath); //上傳目錄的物理路徑 string newFilePath = upLoadPath + newFileName; //上傳後的路徑 string newThumbnailPath = upLoadPath + newThumbnailFileName; //上傳後的縮略圖路徑 //檢查檔案副檔名是否合法 if (!CheckFileExt(fileExt)) { return("{\"status\": 0, \"msg\": \"不允許上傳" + fileExt + "類型的檔案!\"}"); } //檢查檔案大小是否合法 if (!CheckFileSize(fileExt, fileSize)) { return("{\"status\": 0, \"msg\": \"檔案超過限制的大小!\"}"); } //檢查上傳的物理路徑是否存在,不存在則創建 if (!Directory.Exists(fullUpLoadPath)) { Directory.CreateDirectory(fullUpLoadPath); } //儲存檔案 postedFile.SaveAs(fullUpLoadPath + newFileName); //如果是圖片,檢查圖片是否超出最大尺寸,是則裁剪 if (IsImage(fileExt) && (this.siteConfig.imgmaxheight > 0 || this.siteConfig.imgmaxwidth > 0)) { Thumbnail.MakeThumbnailImage(fullUpLoadPath + newFileName, fullUpLoadPath + newFileName, this.siteConfig.imgmaxwidth, this.siteConfig.imgmaxheight); } //如果是圖片,檢查是否需要生成縮略圖,是則生成 if (IsImage(fileExt) && isThumbnail && this.siteConfig.thumbnailwidth > 0 && this.siteConfig.thumbnailheight > 0) { Thumbnail.MakeThumbnailImage(fullUpLoadPath + newFileName, fullUpLoadPath + newThumbnailFileName, this.siteConfig.thumbnailwidth, this.siteConfig.thumbnailheight, "Cut"); } else { newThumbnailPath = newFilePath; //不生成縮略圖則返回原圖 } //如果是圖片,檢查是否需要打浮水印 if (IsWaterMark(fileExt) && isWater) { switch (this.siteConfig.watermarktype) { case 1: WaterMark.AddImageSignText(newFilePath, newFilePath, this.siteConfig.watermarktext, this.siteConfig.watermarkposition, this.siteConfig.watermarkimgquality, this.siteConfig.watermarkfont, this.siteConfig.watermarkfontsize); break; case 2: WaterMark.AddImageSignPic(newFilePath, newFilePath, this.siteConfig.watermarkpic, this.siteConfig.watermarkposition, this.siteConfig.watermarkimgquality, this.siteConfig.watermarktransparency); break; } } //處理完畢,返回JOSN格式的檔資訊 return("{\"status\": 1, \"msg\": \"上傳檔案成功!\", \"name\": \"" + fileName + "\", \"path\": \"" + newFilePath + "\", \"thumb\": \"" + newThumbnailPath + "\", \"size\": " + fileSize + ", \"ext\": \"" + fileExt + "\"}"); } catch { return("{\"status\": 0, \"msg\": \"上傳過程中發生意外錯誤!\"}"); } }
/// <summary> /// 文件上传方法C /// </summary> /// <param name="postedFile">文件流</param> /// <param name="isThumbnail">是否生成缩略图</param> /// <param name="isWater">是否打水印</param> /// <param name="isReOriginal">是否返回文件原名称</param> /// <returns>服务器文件路径</returns> public string fileSaveAs(HttpPostedFile postedFile, bool isThumbnail, bool isWater, bool _isImage, bool _isReOriginal) { try { string fileExt = Utils.GetFileExt(postedFile.FileName); //文件扩展名,不含“.” string originalFileName = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf(@"\") + 1); //取得文件原名 string fileName = Utils.GetRamCode() + "." + fileExt; //随机文件名 string dirPath = GetUpLoadPath(); //上传目录相对路径 //检查文件扩展名是否合法 if (!CheckFileExt(fileExt)) { return("{\"msg\": \"0\", \"msgbox\": \"不允许上传" + fileExt + "类型的文件!\"}"); } //检查是否必须上传图片 if (_isImage && !IsImage(fileExt)) { return("{\"msg\": \"0\", \"msgbox\": \"对不起,仅允许上传图片文件!\"}"); } //检查文件大小是否合法 if (!CheckFileSize(fileExt, postedFile.ContentLength)) { return("{\"msg\": \"0\", \"msgbox\": \"文件超过限制的大小啦!\"}"); } //获得要保存的文件路径 string serverFileName = dirPath + fileName; string serverThumbnailFileName = dirPath + "small_" + fileName; string returnFileName = serverFileName; //物理完整路径 string toFileFullPath = Utils.GetMapPath(dirPath); //检查有该路径是否就创建 if (!Directory.Exists(toFileFullPath)) { Directory.CreateDirectory(toFileFullPath); } //保存文件 postedFile.SaveAs(toFileFullPath + fileName); //如果是图片,检查图片尺寸是否超出限制 if (IsImage(fileExt) && (this.siteConfig.attachimgmaxheight > 0 || this.siteConfig.attachimgmaxwidth > 0)) { Thumbnail.MakeThumbnailImage(toFileFullPath + fileName, toFileFullPath + fileName, this.siteConfig.attachimgmaxwidth, this.siteConfig.attachimgmaxheight); } //是否生成缩略图 if (IsImage(fileExt) && isThumbnail && this.siteConfig.thumbnailwidth > 0 && this.siteConfig.thumbnailheight > 0) { Thumbnail.MakeThumbnailImage(toFileFullPath + fileName, toFileFullPath + "small_" + fileName, this.siteConfig.thumbnailwidth, this.siteConfig.thumbnailheight, "Cut"); returnFileName += "," + serverThumbnailFileName; //返回缩略图,以逗号分隔开 } //是否打图片水印 if (IsWaterMark(fileExt) && isWater) { switch (this.siteConfig.watermarktype) { case 1: WaterMark.AddImageSignText(serverFileName, serverFileName, this.siteConfig.watermarktext, this.siteConfig.watermarkposition, this.siteConfig.watermarkimgquality, this.siteConfig.watermarkfont, this.siteConfig.watermarkfontsize); break; case 2: WaterMark.AddImageSignPic(serverFileName, serverFileName, this.siteConfig.watermarkpic, this.siteConfig.watermarkposition, this.siteConfig.watermarkimgquality, this.siteConfig.watermarktransparency); break; } } //如果需要返回原文件名 if (_isReOriginal) { return("{\"msg\": \"1\", \"msgbox\": \"" + serverFileName + "\", mstitle: \"" + originalFileName + "\"}"); } return("{\"msg\": \"1\", \"msgbox\": \"" + returnFileName + "\"}"); } catch { return("{\"msg\": \"0\", \"msgbox\": \"上传过程中发生意外错误!\"}"); } }
/// <summary> /// 文件上传方法 /// </summary> /// <param name="postedFile">文件流</param> /// <param name="allowExtensions">文件类型</param> /// <param name="maxSize">允许文件最大尺寸</param> /// <param name="isThumbnail">是否生成缩略图</param> /// <param name="isWater">是否打水印</param> /// <returns></returns> public Model.upLoad fileSaveAs(HttpPostedFile postedFile, string[] allowExtensions, int maxSize, bool isThumbnail, bool isWater, int width, int height) { Model.upLoad model = new Model.upLoad(); try { model.ext = Utils.GetFileExt(postedFile.FileName); //文件扩展名,包含“.” model.size = postedFile.ContentLength; //获得文件大小,以字节为单位 model.name = Path.GetFileName(postedFile.FileName); string newFileName = Utils.GetRamCode() + model.ext; //随机生成新的文件名 string newThumbnailFileName = "thumb_" + newFileName; //随机生成缩略图文件名 string upLoadPath = GetUpLoadPath(); //上传目录相对路径 string fullUpLoadPath = Utils.GetMapPath(upLoadPath); //上传目录的物理路径 model.path = upLoadPath + newFileName; //上传后的路径 model.thumb = upLoadPath + newThumbnailFileName; //上传后的缩略图路径 //检查文件扩展名是否合法 if (CheckFileExt(model.ext, allowExtensions)) { //检查文件大小是否合法 if (CheckFileSize(model.size, maxSize)) { //检查上传的物理路径是否存在,不存在则创建 if (!Directory.Exists(fullUpLoadPath)) { Directory.CreateDirectory(fullUpLoadPath); } //保存文件 postedFile.SaveAs(fullUpLoadPath + newFileName); //如果是图片,检查图片是否超出最大尺寸,是则裁剪 if (IsImage(model.ext) && (this.siteConfig.imgmaxheight > 0 || this.siteConfig.imgmaxwidth > 0)) { Thumbnail.MakeThumbnailImage(fullUpLoadPath + newFileName, fullUpLoadPath + newFileName, this.siteConfig.imgmaxwidth, this.siteConfig.imgmaxheight); } //如果是图片,检查是否需要生成缩略图,是则生成 if (isThumbnail && IsImage(model.ext) && this.siteConfig.thumbnailwidth > 0 && this.siteConfig.thumbnailheight > 0) { Thumbnail.MakeThumbnailImage(fullUpLoadPath + newFileName, fullUpLoadPath + newThumbnailFileName, width, height, this.siteConfig.thumbnailmode); } else { model.thumb = ""; } //如果是图片,检查是否需要打水印 if (isWater && IsWaterMark(model.ext)) { switch (this.siteConfig.watermarktype) { case 1: WaterMark.AddImageSignText(model.path, model.path, this.siteConfig.watermarktext, this.siteConfig.watermarkposition, this.siteConfig.watermarkimgquality, this.siteConfig.watermarkfont, this.siteConfig.watermarkfontsize); break; case 2: WaterMark.AddImageSignPic(model.path, model.path, this.siteConfig.watermarkpic, this.siteConfig.watermarkposition, this.siteConfig.watermarkimgquality, this.siteConfig.watermarktransparency); break; } } model.status = 1; model.msg = "上传文件成功!"; } else { model.status = 0; model.msg = "文件超过限制的大小!"; } } else { model.status = 0; model.msg = "不允许上传" + model.ext + "类型的文件!"; } } catch (Exception ex) { model.status = 0; model.msg = ex.Message; LogHelper.WriteLog(ex.ToString()); } return(model); }
/// <summary> /// 通过文件流上传文件方法 /// </summary> /// <param name="byteData">文件字节数组</param> /// <param name="fileName">文件名</param> /// <param name="isThumbnail">是否生成缩略图</param> /// <param name="isWater">是否打水印</param> /// <returns>上传成功返回JSON字符串</returns> public string FileSaveAs(byte[] byteData, string fileName, bool isThumbnail, bool isWater) { try { string fileExt = Path.GetExtension(fileName).Trim('.'); //文件扩展名,不含“.” string newFileName = Utils.GetRamCode() + "." + fileExt; //随机生成新的文件名 string newThumbnailFileName = "thumb_" + newFileName; //随机生成缩略图文件名 string upLoadPath = GetUpLoadPath(); //本地上传目录相对路径 string fullUpLoadPath = Utils.GetMapPath(upLoadPath); //本地上传目录的物理路径 string newFilePath = upLoadPath + newFileName; //本地上传后的路径 string newThumbnailPath = upLoadPath + newThumbnailFileName; //本地上传后的缩略图路径 byte[] thumbData = null; //缩略图文件流 //检查文件字节数组是否为NULL if (byteData == null) { return("{\"status\": 0, \"msg\": \"请选择要上传的文件!\"}"); } //检查文件扩展名是否合法 if (!CheckFileExt(fileExt)) { return("{\"status\": 0, \"msg\": \"不允许上传" + fileExt + "类型的文件!\"}"); } //检查文件大小是否合法 if (!CheckFileSize(fileExt, byteData.Length)) { return("{\"status\": 0, \"msg\": \"文件超过限制的大小!\"}"); } //如果是图片,检查图片是否超出最大尺寸,是则裁剪 if (IsImage(fileExt) && (this.sysConfig.imgmaxheight > 0 || this.sysConfig.imgmaxwidth > 0)) { byteData = Thumbnail.MakeThumbnailImage(byteData, fileExt, this.sysConfig.imgmaxwidth, this.sysConfig.imgmaxheight); } //如果是图片,检查是否需要生成缩略图,是则生成 if (IsImage(fileExt) && isThumbnail && this.sysConfig.thumbnailwidth > 0 && this.sysConfig.thumbnailheight > 0) { thumbData = Thumbnail.MakeThumbnailImage(byteData, fileExt, this.sysConfig.thumbnailwidth, this.sysConfig.thumbnailheight, this.sysConfig.thumbnailmode); } else { newThumbnailPath = newFilePath; //不生成缩略图则返回原图 } //如果是图片,检查是否需要打水印 if (IsWaterMark(fileExt) && isWater) { switch (this.sysConfig.watermarktype) { case 1: byteData = WaterMark.AddImageSignText(byteData, fileExt, this.sysConfig.watermarktext, this.sysConfig.watermarkposition, this.sysConfig.watermarkimgquality, this.sysConfig.watermarkfont, this.sysConfig.watermarkfontsize); break; case 2: byteData = WaterMark.AddImageSignPic(byteData, fileExt, this.sysConfig.watermarkpic, this.sysConfig.watermarkposition, this.sysConfig.watermarkimgquality, this.sysConfig.watermarktransparency); break; } } //分发不同的上传方式处理 switch (sysConfig.fileserver) { case "aliyun": //阿里云OSS对象存储 //检查配置是否完善 if (string.IsNullOrEmpty(sysConfig.osssecretid) || string.IsNullOrEmpty(sysConfig.osssecretkey) || string.IsNullOrEmpty(sysConfig.ossendpoint)) { return("{\"status\": 0, \"msg\": \"文件上传配置未完善,无法上传\"}"); } //初始化阿里云配置 API.Cloud.AliyunOss aliyun = new API.Cloud.AliyunOss(sysConfig.ossendpoint, sysConfig.osssecretid, sysConfig.osssecretkey); string result = string.Empty; //返回信息 //保存主文件 if (!aliyun.PutObject(byteData, sysConfig.ossbucket, newFilePath, sysConfig.ossdomain, out result)) { return("{\"status\": 0, \"msg\": \"" + result + "\"}"); } newFilePath = result; //将地址赋值给新文件地址 //保存缩略图文件 if (thumbData != null) { aliyun.PutObject(thumbData, sysConfig.ossbucket, newThumbnailPath, sysConfig.ossdomain, out result); } break; case "qcloud": //腾讯云COS对象存储 break; default: //本地服务器 //检查本地上传的物理路径是否存在,不存在则创建 if (!Directory.Exists(fullUpLoadPath)) { Directory.CreateDirectory(fullUpLoadPath); } //保存主文件 FileHelper.SaveFile(byteData, fullUpLoadPath + newFileName); //保存缩略图文件 if (thumbData != null) { FileHelper.SaveFile(thumbData, fullUpLoadPath + newThumbnailFileName); } break; } //处理完毕,返回JOSN格式的文件信息 return("{\"status\": 1, \"msg\": \"上传文件成功!\", \"name\": \"" + fileName + "\", \"path\": \"" + newFilePath + "\", \"thumb\": \"" + newThumbnailPath + "\", \"size\": " + byteData.Length + ", \"ext\": \"" + fileExt + "\"}"); } catch (Exception err) { return("{\"status\": 0, \"msg\": \"上传过程中发生意外错误!\"}"); } }
/// <summary> /// 文件上传方法 /// </summary> /// <param name="postedFile">文件类型</param> /// <param name="isThumbnail">是否缩略图</param> /// <param name="isWater">是否增加水印</param> /// <returns></returns> public UploadFileInfo UploadFile(IFormFile fileinput, bool isThumbnail = false, bool isWater = false) { var filepath = ContentDispositionHeaderValue.Parse(fileinput.ContentDisposition).FileName.Trim('"'); long fileSize = 0; //string fileExt = filepath.Substring(filepath.LastIndexOf('.')).Replace("\"", ""); string fileExt = fileinput.FileName.Substring(fileinput.FileName.LastIndexOf('.') + 1, fileinput.FileName.Length - fileinput.FileName.LastIndexOf('.') - 1); //string fileExt = fileinput.FileName.Substring(filepath.LastIndexOf('.')).Replace("\"", ""); string fileName = filepath.Substring(filepath.LastIndexOf(@"\", System.StringComparison.Ordinal) + 1); //取得原文件名 //string fileExt =FileHelper.GetFileExt(postedFile.FileName); //文件扩展名,不含“.” //int fileSize = postedFile.ContentLength; //获得文件大小,以字节为单位 //string fileName = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf(@"\", System.StringComparison.Ordinal) + 1); //取得原文件名 string newFileName = Utils.GetCheckCode(20).ToLower() + "." + fileExt; //随机生成新的文件名 string newThumbnailFileName = "thumb_" + newFileName; //随机生成缩略图文件名 string upLoadPath = AssigendPath(fileExt, _sysconfig.filerootpath + "/" + _sysconfig.filepath); //上传目录相对路径 string fullUpLoadPath = Utils.GetMapPath(upLoadPath); //上传目录的物理路径 string newFilePath = upLoadPath + newFileName; //上传后的路径 string newThumbnailPath = upLoadPath + newThumbnailFileName; //上传后的缩略图路径 fileSize += fileinput.Length; //检查上传的物理路径是否存在,不存在则创建 if (!Directory.Exists(fullUpLoadPath)) { Directory.CreateDirectory(fullUpLoadPath); } using (FileStream fs = System.IO.File.Create(fullUpLoadPath + newFileName)) { fileinput.CopyTo(fs); fs.Flush(); } //保存文件 //postedFile.SaveAs(fullUpLoadPath + newFileName); //如果是图片,检查图片是否超出最大尺寸,是则裁剪 if (IsImage(fileExt) && (this._sysconfig.imgmaxheight > 0 || this._sysconfig.imgmaxwidth > 0)) { Thumbnail.MakeThumbnailImage(fullUpLoadPath + newFileName, fullUpLoadPath + newFileName, this._sysconfig.imgmaxwidth, this._sysconfig.imgmaxheight); } //如果是图片,检查是否需要生成缩略图,是则生成 if (IsImage(fileExt) && isThumbnail && this._sysconfig.thumbnailwidth > 0 && this._sysconfig.thumbnailheight > 0) { Thumbnail.MakeThumbnailImage(fullUpLoadPath + newFileName, fullUpLoadPath + newThumbnailFileName, this._sysconfig.thumbnailwidth, this._sysconfig.thumbnailheight, "H"); } //如果是图片,检查是否需要打水印 if (IsWaterMark(fileExt) && isWater) { switch (this._sysconfig.watermarktype) { case 1: //WaterMark.AddImageSignText(newFilePath, newFilePath, // this._sysconfig.watermarktext, this._sysconfig.watermarkposition, // this._sysconfig.watermarkimgquality, this._sysconfig.watermarkfont, this._sysconfig.watermarkfontsize); WaterMark.AddWaterMark(newFilePath, newFilePath, this._sysconfig.watermarktext, this._sysconfig.watermarkposition, this._sysconfig.watermarkimgquality, this._sysconfig.watermarkfont, this._sysconfig.watermarkfontsize); break; case 2: var watermarkpic = this._sysconfig.watermarkpic; if (!watermarkpic.Contains("wwwroot")) { watermarkpic = "/wwwroot" + watermarkpic; } WaterMark.AddImageSignPic(newFilePath, newFilePath, watermarkpic, this._sysconfig.watermarkposition, this._sysconfig.watermarkimgquality, this._sysconfig.watermarktransparency); break; } } var m = new UploadFileInfo() { OriginalName = filepath, Size = FileSizeTransf(fileSize), FileName = newFilePath, Name = newFileName, ThumbFileName = newThumbnailPath, ThumbName = newThumbnailFileName, FileExt = "." + fileExt, Paths = newFilePath }; return(m); }
/// <summary> /// 文件上传方法 /// </summary> /// <param name="postedFile">文件类型</param> /// <param name="isThumbnail">是否缩略图</param> /// <param name="isWater">是否增加水印</param> /// <returns></returns> public UploadFileInfo UploadFile(HttpPostedFileBase postedFile, bool isThumbnail = false, bool isWater = false) { string fileExt = FileHelper.GetFileName(postedFile.FileName); //文件扩展名,不含“.” int fileSize = postedFile.ContentLength; //获得文件大小,以字节为单位 string fileName = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf(@"\", System.StringComparison.Ordinal) + 1); //取得原文件名 string newFileName = Utils.GetCheckCode(20).ToLower() + "." + fileExt; //随机生成新的文件名 string newThumbnailFileName = "thumb_" + newFileName; //随机生成缩略图文件名 string upLoadPath = AssigendPath(fileExt, _sysconfig.filepath); //上传目录相对路径 string fullUpLoadPath = Utils.GetMapPath(upLoadPath); //上传目录的物理路径 string newFilePath = upLoadPath + newFileName; //上传后的路径 string newThumbnailPath = upLoadPath + newThumbnailFileName; //上传后的缩略图路径 //检查上传的物理路径是否存在,不存在则创建 if (!Directory.Exists(fullUpLoadPath)) { Directory.CreateDirectory(fullUpLoadPath); } //保存文件 postedFile.SaveAs(fullUpLoadPath + newFileName); //如果是图片,检查图片是否超出最大尺寸,是则裁剪 if (IsImage(fileExt) && (this._sysconfig.imgmaxheight > 0 || this._sysconfig.imgmaxwidth > 0)) { Thumbnail.MakeThumbnailImage(fullUpLoadPath + newFileName, fullUpLoadPath + newFileName, this._sysconfig.imgmaxwidth, this._sysconfig.imgmaxheight); } //如果是图片,检查是否需要生成缩略图,是则生成 if (IsImage(fileExt) && isThumbnail && this._sysconfig.thumbnailwidth > 0 && this._sysconfig.thumbnailheight > 0) { Thumbnail.MakeThumbnailImage(fullUpLoadPath + newFileName, fullUpLoadPath + newThumbnailFileName, this._sysconfig.thumbnailwidth, this._sysconfig.thumbnailheight, "Cut"); } //如果是图片,检查是否需要打水印 if (IsWaterMark(fileExt) && isWater) { switch (this._sysconfig.watermarktype) { case 1: WaterMark.AddImageSignText(newFilePath, newFilePath, this._sysconfig.watermarktext, this._sysconfig.watermarkposition, this._sysconfig.watermarkimgquality, this._sysconfig.watermarkfont, this._sysconfig.watermarkfontsize); break; case 2: WaterMark.AddImageSignPic(newFilePath, newFilePath, this._sysconfig.watermarkpic, this._sysconfig.watermarkposition, this._sysconfig.watermarkimgquality, this._sysconfig.watermarktransparency); break; } } var m = new UploadFileInfo() { OriginalName = postedFile.FileName, Size = FileSizeTransf(fileSize), FileName = newFilePath, Name = newFileName, ThumbFileName = newThumbnailPath, ThumbName = newThumbnailFileName, FileExt = "." + fileExt, Paths = newFilePath }; return(m); }
/// <summary> /// 文件上传方法 /// </summary> /// <param name="postedFile">文件流</param> /// <param name="isThumbnail">是否生成缩略图</param> /// <param name="isWater">是否生成水印</param> /// <returns>上传后文件信息</returns> private JsonHelper FileSaveAs(HttpPostedFileBase postedFile, bool isThumbnail, bool isWater) { var jsons = new JsonHelper { Status = "n" }; try { string fileExt = Utils.GetFileExt(postedFile.FileName); //文件扩展名,不含“.” int fileSize = postedFile.ContentLength; //获得文件大小,以字节为单位 string fileName = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf(@"\") + 1); //取得原文件名 string newFileName = Utils.GetRamCode() + "." + fileExt; //随机生成新的文件名 string upLoadPath = GetUpLoadPath(fileExt); //上传目录相对路径 string fullUpLoadPath = Utils.GetMapPath(upLoadPath); //上传目录的物理路径 string newFilePath = upLoadPath + newFileName; //上传后的路径 string newThumbnailFileName = "thumb_" + newFileName; //随机生成缩略图文件名 //检查文件扩展名是否合法 if (!CheckFileExt(fileExt)) { jsons.Msg = "不允许上传" + fileExt + "类型的文件!"; return(jsons); } //检查文件大小是否合法 if (!CheckFileSize(fileExt, fileSize)) { jsons.Msg = "文件超过限制的大小啦!"; return(jsons); } //检查上传的物理路径是否存在,不存在则创建 if (!Directory.Exists(fullUpLoadPath)) { Directory.CreateDirectory(fullUpLoadPath); } //检查文件是否真实合法 //如果文件真实合法 则 保存文件 关闭文件流 //if (!CheckFileTrue(postedFile, fullUpLoadPath + newFileName)) //{ // jsons.Msg = "不允许上传不可识别的文件!"; // return jsons; //} //保存文件 postedFile.SaveAs(fullUpLoadPath + newFileName); string thumbnail = string.Empty; //如果是图片,检查是否需要生成缩略图,是则生成 if (IsImage(fileExt) && isThumbnail && ConfigurationManager.AppSettings["ThumbnailWidth"].ToString() != "0" && ConfigurationManager.AppSettings["ThumbnailHeight"].ToString() != "0") { Thumbnail.MakeThumbnailImage(fullUpLoadPath + newFileName, fullUpLoadPath + newThumbnailFileName, int.Parse(ConfigurationManager.AppSettings["ThumbnailWidth"]), int.Parse(ConfigurationManager.AppSettings["ThumbnailHeight"]), "W"); thumbnail = upLoadPath + newThumbnailFileName; } //如果是图片,检查是否需要打水印 if (IsImage(fileExt) && isWater) { switch (ConfigurationManager.AppSettings["WatermarkType"].ToString()) { case "1": WaterMark.AddImageSignText(newFilePath, newFilePath, ConfigurationManager.AppSettings["WatermarkText"], int.Parse(ConfigurationManager.AppSettings["WatermarkPosition"]), int.Parse(ConfigurationManager.AppSettings["WatermarkImgQuality"]), ConfigurationManager.AppSettings["WatermarkFont"], int.Parse(ConfigurationManager.AppSettings["WatermarkFontsize"])); break; case "2": WaterMark.AddImageSignPic(newFilePath, newFilePath, ConfigurationManager.AppSettings["WatermarkPic"], int.Parse(ConfigurationManager.AppSettings["WatermarkPosition"]), int.Parse(ConfigurationManager.AppSettings["WatermarkImgQuality"]), int.Parse(ConfigurationManager.AppSettings["WatermarkTransparency"])); break; } } string unit = string.Empty; //处理完毕,返回JOSN格式的文件信息 jsons.Data = "{\"oldname\": \"" + fileName + "\","; //原始文件名 jsons.Data += " \"newname\":\"" + newFileName + "\","; //文件新名称 jsons.Data += " \"path\": \"" + newFilePath + "\", "; //文件路径 jsons.Data += " \"thumbpath\":\"" + thumbnail + "\","; //缩略图路径 jsons.Data += " \"size\": \"" + fileSize + "\","; //文件大小 jsons.Data += "\"ext\":\"" + fileExt + "\"}"; //文件格式 jsons.Status = "y"; return(jsons); } catch { jsons.Msg = "上传过程中发生意外错误!"; return(jsons); } }
/// <summary> /// 文件上传方法-重载 /// 创建:MJZ /// 时间:2014年7月30日 19:16:39 /// </summary> /// <param name="postedFile">文件流</param> /// <param name="isThumbnail">是否生成缩略图</param> /// <param name="isWater">是否打水印</param> /// <param name="imgWidth">图片压缩宽度</param> /// <param name="imgHeight">图片压缩高度</param> /// <param name="isSaveFullPath">是否保存全路径</param> /// <returns>上传后文件信息</returns> public string FileSaveAs(HttpPostedFile postedFile, bool isThumbnail, bool isWater, int imgWidth, int imgHeight, bool isSaveFullPath) { try { var fileExt = Utils.GetFileExt(postedFile.FileName); //文件扩展名,不含“.” var fileSize = postedFile.ContentLength; //获得文件大小,以字节为单位 var fileName = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf(@"\", StringComparison.Ordinal) + 1); //取得原文件名 var newFileName = Utils.GetRamCode() + "." + fileExt; //随机生成新的文件名 var newThumbnailFileName = "thumb_" + newFileName; //随机生成缩略图文件名 var upLoadPath = GetUpLoadPath(); //上传目录相对路径 var fullUpLoadPath = Utils.GetMapPath(upLoadPath); //上传目录的物理路径 var newFilePath = upLoadPath + newFileName; //上传后的路径 var newThumbnailPath = upLoadPath + newThumbnailFileName; //上传后的缩略图路径 /* *不需要检查文件大小和类型,因为控件已经检查过了。 *检查上传的物理路径是否存在,不存在则创建 */ if (!Directory.Exists(fullUpLoadPath)) { Directory.CreateDirectory(fullUpLoadPath); } //保存文件 postedFile.SaveAs(fullUpLoadPath + newFileName); //如果是图片,检查是否需要生成缩略图,是则生成 if (IsImage(fileExt) && isThumbnail && imgWidth > 0 && imgHeight > 0) { Thumbnail.MakeThumbnailImage(fullUpLoadPath + newFileName, fullUpLoadPath + newThumbnailFileName, imgWidth, imgHeight, "W"); } //如果是图片,检查是否需要打水印 if (IsWaterMark(fileExt) && isWater) { switch (_siteConfig.Watermarktype) { case 1: WaterMark.AddImageSignText(newFilePath, newFilePath, _siteConfig.Watermarktext, _siteConfig.Watermarkposition, _siteConfig.Watermarkimgquality, _siteConfig.Watermarkfont, _siteConfig.Watermarkfontsize); break; case 2: WaterMark.AddImageSignPic(newFilePath, newFilePath, _siteConfig.Watermarkpic, _siteConfig.Watermarkposition, _siteConfig.Watermarkimgquality, _siteConfig.Watermarktransparency); break; } } if (!isSaveFullPath) { return("{\"status\": 1, \"msg\": \"上传文件成功!\", \"name\": \"" + fileName + "\", \"path\": \"" + newFilePath + "\", \"thumb\": \"" + newThumbnailPath + "\", \"size\": " + fileSize + ", \"ext\": \"" + fileExt + "\"}"); } newFilePath = "http://" + HttpContext.Request.ServerVariables["HTTP_HOST"] + newFilePath; newThumbnailPath = "http://" + HttpContext.Request.ServerVariables["HTTP_HOST"] + newThumbnailPath; //处理完毕,返回JOSN格式的文件信息 return("{\"status\": 1, \"msg\": \"上传文件成功!\", \"name\": \"" + fileName + "\", \"path\": \"" + newFilePath + "\", \"thumb\": \"" + newThumbnailPath + "\", \"size\": " + fileSize + ", \"ext\": \"" + fileExt + "\"}"); } catch { return("{\"status\": 0, \"msg\": \"上传过程中发生意外错误!\"}"); } }
/// <summary> /// 文件上传方法 /// </summary> /// <param name="postedFile">文件流</param> /// <param name="isThumbnail">是否生成缩略图</param> /// <param name="isWater">是否打水印</param> /// <returns>上传后文件信息</returns> public string fileSaveAs(HttpPostedFile postedFile, bool isThumbnail, bool isWater) { try { string fileExt = Utils.GetFileExt(postedFile.FileName); //文件扩展名,不含“.” int fileSize = postedFile.ContentLength; //获得文件大小,以字节为单位 string fileName = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf(@"\") + 1); //取得原文件名 string newFileName = Utils.GetRamCode() + "." + fileExt; //随机生成新的文件名 string newThumbnailFileName = "thumb_" + newFileName; //随机生成缩略图文件名 string upLoadPath = GetUpLoadPath(); //上传目录相对路径 string fullUpLoadPath = Utils.GetMapPath(upLoadPath); //上传目录的物理路径 string newFilePath = upLoadPath + newFileName; //上传后的路径 string newThumbnailPath = upLoadPath + newThumbnailFileName; //上传后的缩略图路径 //检查文件扩展名是否合法 if (!CheckFileExt(fileExt)) { return("{\"status\": 0, \"msg\": \"不允许上传" + fileExt + "类型的文件!\"}"); } //检查文件大小是否合法 if (!CheckFileSize(fileExt, fileSize)) { return("{\"status\": 0, \"msg\": \"文件超过限制的大小啦!\"}"); } //检查上传的物理路径是否存在,不存在则创建 if (!Directory.Exists(fullUpLoadPath)) { Directory.CreateDirectory(fullUpLoadPath); } //保存文件 postedFile.SaveAs(fullUpLoadPath + newFileName); //如果是图片,检查图片是否超出最大尺寸,是则裁剪 if (IsImage(fileExt) && (this.siteConfig.imgmaxheight > 0 || this.siteConfig.imgmaxwidth > 0)) { Thumbnail.MakeThumbnailImage(fullUpLoadPath + newFileName, fullUpLoadPath + newFileName, this.siteConfig.imgmaxwidth, this.siteConfig.imgmaxheight); } //如果是图片,检查是否需要生成缩略图,是则生成 if (IsImage(fileExt) && isThumbnail && this.siteConfig.thumbnailwidth > 0 && this.siteConfig.thumbnailheight > 0) { Thumbnail.MakeThumbnailImage(fullUpLoadPath + newFileName, fullUpLoadPath + newThumbnailFileName, this.siteConfig.thumbnailwidth, this.siteConfig.thumbnailheight, "Cut"); } //如果是图片,检查是否需要打水印 if (IsWaterMark(fileExt) && isWater) { switch (this.siteConfig.watermarktype) { case 1: WaterMark.AddImageSignText(newFilePath, newFilePath, this.siteConfig.watermarktext, this.siteConfig.watermarkposition, this.siteConfig.watermarkimgquality, this.siteConfig.watermarkfont, this.siteConfig.watermarkfontsize); break; case 2: WaterMark.AddImageSignPic(newFilePath, newFilePath, this.siteConfig.watermarkpic, this.siteConfig.watermarkposition, this.siteConfig.watermarkimgquality, this.siteConfig.watermarktransparency); break; } } //处理完毕,返回JOSN格式的文件信息 return("{\"status\": 1, \"msg\": \"上传文件成功!\", \"name\": \"" + fileName + "\", \"path\": \"" + newFilePath + "\", \"thumb\": \"" + newThumbnailPath + "\", \"size\": " + fileSize + ", \"ext\": \"" + fileExt + "\"}"); } catch { return("{\"status\": 0, \"msg\": \"上传过程中发生意外错误!\"}"); } }
/// <summary> /// 通过文件流上传文件方法 /// </summary> /// <param name="upType">文件上传类型</param> /// <param name="byteData">文件字节数组</param> /// <param name="fileName">文件名</param> /// <param name="isThumbnail">是否生成缩略图</param> /// <param name="isWater">是否打水印</param> /// <param name="isCompress">是否启用图片压缩</param> /// <param name="isCover">是否为封面</param> /// <param name="thumbnailWidth">缩略图宽度</param> /// <param name="thumbnailHeight">缩略图高度</param> /// <param name="thumbnailMode">缩略图生成方式</param> /// <returns>上传成功返回JSON字符串</returns> public string StrFileSaveAs(string upType, byte[] byteData, string fileName, bool isThumbnail, bool isWater, bool isCompress, bool isCover, int thumbnailWidth, int thumbnailHeight, string thumbnailMode) { try { int maxSize = 0; string[] allowExtensions = null; //上传类型 switch (upType.ToLower()) { case "file": maxSize = siteConfig.attachsize; allowExtensions = siteConfig.attachextension.Split(','); break; case "video": //视频上传 maxSize = siteConfig.videosize; allowExtensions = siteConfig.videoextension.Split(','); break; default: maxSize = siteConfig.imgsize; allowExtensions = siteConfig.imgextension.Split(','); break; } string fileExt = Path.GetExtension(fileName).Trim('.').ToLower(); //文件扩展名,不含“.” string newFileName = Utils.GetRamCode() + "." + fileExt; //随机生成新的文件名 string newThumbnailFileName = "thumb_" + newFileName; //随机生成缩略图文件名 string upLoadPath = GetUpLoadPath(); //本地上传目录相对路径 string fullUpLoadPath = Utils.GetMapPath(upLoadPath); //本地上传目录的物理路径 string newFilePath = upLoadPath + newFileName; //本地上传后的路径 string newThumbnailPath = upLoadPath + newThumbnailFileName; //本地上传后的缩略图路径 byte[] thumbData = null; //缩略图文件流 //检查文件字节数组是否为NULL if (byteData == null) { return("{\"status\": 0, \"msg\": \"请选择要上传的文件!\"}"); } //检查文件扩展名是否合法 if (!CheckFileExt(fileExt, allowExtensions)) { return("{\"status\": 0, \"msg\": \"不允许上传" + fileExt + "类型的文件!\"}"); } //检查文件大小是否合法 if (!CheckFileSize(byteData.Length, maxSize)) { return("{\"status\": 0, \"msg\": \"文件超过限制的大小!\"}"); } //判断是否为图片存储 if (upType.ToLower() == "img") { //是否是生成封面 if (isCover) { //封面直接裁剪 if (thumbnailWidth > 0 && thumbnailHeight > 0) { byteData = Thumbnail.MakeThumbnailImage(byteData, fileExt, thumbnailWidth, thumbnailHeight, thumbnailMode); } } else { //如果是图片,检查图片是否超出最大尺寸,是则裁剪 if (isCompress && this.siteConfig.imgmaxheight > 0 || this.siteConfig.imgmaxwidth > 0) { byteData = Thumbnail.MakeThumbnailImage(byteData, fileExt, this.siteConfig.imgmaxwidth, this.siteConfig.imgmaxheight); } } //如果是图片,检查是否需要生成缩略图,是则生成 if (isThumbnail && thumbnailWidth > 0 && thumbnailHeight > 0) { thumbData = Thumbnail.MakeThumbnailImage(byteData, fileExt, thumbnailWidth, thumbnailHeight, thumbnailMode); } //else //{ // newThumbnailPath = newFilePath; //不生成缩略图则返回原图 //} //如果是图片,检查是否需要打水印 if (isWater && siteConfig.watermarktype > 0 && IsWaterMark(fileExt)) { switch (this.siteConfig.watermarktype) { case 1: if (!string.IsNullOrEmpty(this.siteConfig.watermarktext)) { byteData = WaterMark.AddImageSignText(byteData, fileExt, this.siteConfig.watermarktext, this.siteConfig.watermarkposition, this.siteConfig.watermarkimgquality, this.siteConfig.watermarkfont, this.siteConfig.watermarkfontsize); } break; case 2: if (!string.IsNullOrEmpty(this.siteConfig.watermarkpic)) { byteData = WaterMark.AddImageSignPic(byteData, fileExt, this.siteConfig.watermarkpic, this.siteConfig.watermarkposition, this.siteConfig.watermarkimgquality, this.siteConfig.watermarktransparency); } break; } } //图片处理插件选择 if (siteConfig.imgplugin > 0) { //主图 MagickHelper mhr = new MagickHelper(); mhr.byteData = byteData; mhr.quality = 0; if (isCompress) { mhr.quality = siteConfig.watermarkimgquality; } //是否需要转换扩展名 if (!string.IsNullOrEmpty(siteConfig.imgconvert)) { fileExt = siteConfig.imgconvert; mhr.fileExt = siteConfig.imgconvert; //替换扩展名 newFileName = newFileName.Remove(newFileName.LastIndexOf(".") + 1) + fileExt; newThumbnailFileName = newThumbnailFileName.Remove(newThumbnailFileName.LastIndexOf(".") + 1) + fileExt; newFilePath = newFilePath.Remove(newFilePath.LastIndexOf(".") + 1) + fileExt; newThumbnailPath = newThumbnailPath.Remove(newThumbnailPath.LastIndexOf(".") + 1) + fileExt; } byteData = mhr.Compress(); //缩略图 if (thumbData != null) { MagickHelper mhr2 = new MagickHelper(); mhr2.byteData = thumbData; mhr2.quality = mhr.quality < 90 ? 90 : mhr.quality; mhr2.fileExt = mhr.fileExt; } } } else if (upType.ToLower() == "video") { } //分发不同的上传方式处理 switch (siteConfig.fileserver) { case "aliyun": //阿里云OSS对象存储 #region 阿里云OSS对象存储 //检查配置是否完善 if (string.IsNullOrEmpty(siteConfig.accessid) || string.IsNullOrEmpty(siteConfig.accesssecret) || string.IsNullOrEmpty(siteConfig.endpoint)) { return("{\"status\": 0, \"msg\": \"文件上传配置未完善,无法上传\"}"); } //初始化阿里云配置 DTcms.Cloud.AliyunOss aliyun = new DTcms.Cloud.AliyunOss(siteConfig.endpoint, siteConfig.accessid, siteConfig.accesssecret); string result = string.Empty; //返回信息 //保存主文件 if (!aliyun.PutObject(byteData, siteConfig.spacename, newFilePath, siteConfig.spaceurl, out result)) { return("{\"status\": 0, \"msg\": \"" + result + "\"}"); } newFilePath = result; //将地址赋值给新文件地址 //保存缩略图文件 if (thumbData != null) { aliyun.PutObject(thumbData, siteConfig.spacename, newThumbnailPath, siteConfig.spaceurl, out result); newThumbnailPath = result; //将缩略图地址赋值 } else { newThumbnailPath = newFilePath; //没有缩略图将原图返回 } #endregion break; case "qcloud": //腾讯云COS对象存储 #region 腾讯云COS对象存储 #endregion break; case "domain": //本地跨域存储 #region 本地跨域存储 fullUpLoadPath = siteConfig.domainpath + upLoadPath.Replace("/", "\\"); var bindDomain = siteConfig.domainbind; if (bindDomain.EndsWith("/")) { bindDomain = bindDomain.Remove(bindDomain.Length - 1); } newFilePath = bindDomain + newFilePath; //检查本地上传的物理路径是否存在,不存在则创建 if (!Directory.Exists(fullUpLoadPath)) { Directory.CreateDirectory(fullUpLoadPath); } //保存主文件 FileHelper.SaveFile(byteData, fullUpLoadPath + newFileName); //保存缩略图文件 if (thumbData != null) { newThumbnailPath = bindDomain + newThumbnailPath; FileHelper.SaveFile(thumbData, fullUpLoadPath + newThumbnailFileName); } else { newThumbnailPath = newFilePath; } #endregion break; default: //本地服务器 #region 本地服务器 //检查本地上传的物理路径是否存在,不存在则创建 if (!Directory.Exists(fullUpLoadPath)) { Directory.CreateDirectory(fullUpLoadPath); } //保存主文件 FileHelper.SaveFile(byteData, fullUpLoadPath + newFileName); //保存缩略图文件 if (thumbData != null) { FileHelper.SaveFile(thumbData, fullUpLoadPath + newThumbnailFileName); } else { newThumbnailPath = upLoadPath; } #endregion break; } //处理完毕,返回JOSN格式的文件信息 return("{\"status\": 1, \"msg\": \"上传文件成功!\", \"name\": \"" + fileName + "\", \"path\": \"" + newFilePath + "\", \"thumb\": \"" + newThumbnailPath + "\", \"size\": " + byteData.Length + ", \"ext\": \"" + fileExt + "\"}"); } catch (Exception ex) { LogHelper.Info(ex.Message); return("{\"status\": 0, \"msg\": \"上传过程中发生意外错误!\"}"); } }