/// <summary> /// 文件存在性 /// </summary> /// <returns></returns> public bool FileExist(string filefullname) { string filepath = filefullname.Substring(0, filefullname.LastIndexOf("/") + 1); string filename = filefullname.Remove(0, filepath.Length); string Fileparentpath = BaseOp.WebPathTran(filepath); return(File.Exists(Fileparentpath + filename)); }
/// <summary> /// 删除目录(包括子文件及文件夹) /// </summary> /// <param name="parentPath">父级目录</param> /// <param name="DirName">文件夹名</param> public bool DeleteDir(string parentPath, string DirName) { try { Directory.Delete(BaseOp.WebPathTran(parentPath) + DirName, true); return(true); } catch (Exception) { return(false); } }
/// <summary> /// 文件删除 /// </summary> /// <param name="parentPath">所在父级目录</param> /// <param name="filename">文件名</param> /// <returns></returns> public static bool DeleteFile(string parentPath, string filename) { try { string filefullname = BaseOp.WebPathTran(parentPath) + filename; File.Delete(filefullname); return(true); } catch { return(false); } }
/// <summary> /// 文件下载 /// </summary> /// <param name="parentpath">所在目录</param> /// <param name="filename">文件名</param> /// <returns></returns> public static void DownLoadFile(string parentpath, string filename, HttpContext httpcontext) { string Filepath = BaseOp.WebPathTran(parentpath); HttpResponse response = httpcontext.Response; HttpRequest request = httpcontext.Request; if (File.Exists(Filepath + filename)) { response.ContentType = "application/octet-stream"; response.AddHeader("Content-Disposition", "attachment;filename=" + httpcontext.Server.UrlEncode(filename)); response.WriteFile(Filepath + filename); } else { response.Redirect(request.ServerVariables["HTTP_REFERER"]); } }
/// <summary> /// 图片格式转换 /// </summary> /// <param name="OriFilename">原始文件相对路径</param> /// <param name="DesiredFilename">生成目标文件相对路径</param> /// <returns></returns> /// JPG采用的是有损压缩所以JPG图像有可能会降低图像清晰度,而像素是不会降低的 /// GIF采用的是无损压缩所以GIF图像是不会降低原图图像清晰度和像素的,但是GIF格式只支持256色图像。 public static bool ConvertImage(string OriFilename, string DesiredFilename) { string extname = DesiredFilename.Substring(DesiredFilename.LastIndexOf('.') + 1).ToLower(); ImageFormat DesiredFormat; //根据扩张名,指定ImageFormat switch (extname) { case "bmp": DesiredFormat = ImageFormat.Bmp; break; case "gif": DesiredFormat = ImageFormat.Gif; break; case "jpeg": DesiredFormat = ImageFormat.Jpeg; break; case "ico": DesiredFormat = ImageFormat.Icon; break; case "png": DesiredFormat = ImageFormat.Png; break; default: DesiredFormat = ImageFormat.Jpeg; break; } try { System.Drawing.Image imgFile = System.Drawing.Image.FromFile(BaseOp.WebPathTran(OriFilename)); imgFile.Save(BaseOp.WebPathTran(DesiredFilename), DesiredFormat); return(true); } catch { return(false); } }
/// <summary> /// 小文件下载 /// </summary> /// <param name="filefullname">完整文件名</param> public static void DownLoadSmallFile(string filefullname, HttpContext httpcontext) { HttpResponse response = httpcontext.Response; HttpRequest request = httpcontext.Request; string filepath = filefullname.Substring(0, filefullname.LastIndexOf("/") + 1); string filename = filefullname.Remove(0, filepath.Length); string Fileparentpath = BaseOp.WebPathTran(filepath); if (File.Exists(Fileparentpath + filename)) { response.ContentType = "application/octet-stream"; response.AddHeader("Content-Disposition", "attachment;filename=" + httpcontext.Server.UrlEncode(filename)); response.TransmitFile(Fileparentpath + filename); } else { response.Redirect(request.ServerVariables["HTTP_REFERER"]); } }
/// <summary> /// 上传文件(默认文件名) /// </summary> /// <param name="fileUploadControl">上传控件ID</param> /// <param name="SavePath">指定目录</param> /// <param name="IsTimeName">启用时间重命名</param> /// <returns></returns> public bool UploadFile(FileUpload fileUploadControl, string SavePath, bool IsTimeName) { string filename = fileUploadControl.FileName; if (IsTimeName) { //时间+扩展名 filename = DateTime.Now.Ticks.ToString() + filename.Substring(filename.LastIndexOf(".")); } try { string FileInServerName = BaseOp.WebPathTran(SavePath) + filename; fileUploadControl.SaveAs(FileInServerName); return(true); } catch (Exception ex) { throw ex; return(false); } }
/// <summary> /// 图片固定大小缩放 /// </summary> /// <param name="OriFileName">源文件相对地址</param> /// <param name="DesiredFilename">目标文件相对地址</param> /// <param name="IntWidth">目标文件宽</param> /// <param name="IntHeight">目标文件高</param> /// <param name="imageFormat">图片文件格式</param> public static bool ChangeImageSize(string OriFileName, string DesiredFilename, int IntWidth, int IntHeight, ImageFormat imageFormat) { string SourceFileNameStr = BaseOp.WebPathTran(OriFileName); //来源图片名称路径 string TransferFileNameStr = BaseOp.WebPathTran(DesiredFilename); //目的图片名称路径 FileStream myOutput = null; try { System.Drawing.Image.GetThumbnailImageAbort myAbort = new System.Drawing.Image.GetThumbnailImageAbort(imageAbort); Image SourceImage = System.Drawing.Image.FromFile(OriFileName); //来源图片定义 Image TargetImage = SourceImage.GetThumbnailImage(IntWidth, IntHeight, myAbort, IntPtr.Zero); //目的图片定义 //将TargetFileNameStr的图片放宽为IntWidth,高为IntHeight myOutput = new FileStream(TransferFileNameStr, FileMode.Create, FileAccess.Write, FileShare.Write); TargetImage.Save(myOutput, imageFormat); myOutput.Close(); return(true); } catch { myOutput.Close(); return(false); } }