博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
文件操作帮助类
阅读量:4339 次
发布时间:2019-06-07

本文共 3845 字,大约阅读时间需要 12 分钟。

文件操作帮助类:

///     /// 对文件上传操作的帮助类    ///     public class FileManager    {        ///         /// 上传文件目录名称        ///         private static string FileFolder = ConfigurationSettings.AppSettings["UploadFolder"];        ///         /// 上传文件基础目录        ///         private static string BaseFolder = HttpContext.Current.Server.MapPath("~/"+FileFolder);        ///         /// 根网站        ///         private static string RootSite = ConfigurationSettings.AppSettings["RootSite"];        ///         /// 文件基础路径        ///         public static string FileBaseUrl = RootSite + "/" + FileFolder + "/";        ///         /// 上传文件        ///         ///         /// 
文件上传后的url
public static string UploadFile(HttpPostedFileBase file) { string extension = Path.GetExtension(file.FileName); if(string.IsNullOrEmpty(extension)) { return string.Empty; } string childFolder = extension.Substring(1); string folderName = Path.Combine(BaseFolder, childFolder); CreateFile(folderName); string fileName=Guid.NewGuid() + extension; string fileFullName = Path.Combine(folderName, fileName); CreateFileStream(fileFullName,file); return childFolder + "/" + fileName; } /// /// /// /// /// public static void CreateFileStream(string fileFullName, HttpPostedFileBase file) { using (FileStream fs = new FileStream(fileFullName, FileMode.OpenOrCreate)) { byte[] buffer = new byte[file.ContentLength]; file.InputStream.Read(buffer, 0, file.ContentLength); fs.Write(buffer, 0, file.ContentLength); fs.Flush(); fs.Close(); } } /// /// 创建对象 /// /// public static void CreateFile(string folderName) { DirectoryInfo di = new DirectoryInfo(folderName); if (!di.Exists) { di.Create(); } } /// /// 删除文件 /// /// UploadFile返回的字符串 public static void DeleteFile(string fileRelativePath) { if (string.IsNullOrEmpty(fileRelativePath )) { return; } string fileName = Path.Combine(BaseFolder, fileRelativePath); FileInfo fi = new FileInfo(fileName); if (fi.Exists) { fi.Delete(); } } /// /// 下载文件 /// /// 文件相对路径 public static void DownLoadFile(string fileRelativePath) { string fileName = fileRelativePath.Substring(fileRelativePath.LastIndexOf('/')); string filePath = Path.Combine(BaseFolder, fileRelativePath); //将FileInfo类使用改为File类,将using()作用范围去除,因为与fs.close()重复 if (!System.IO.File.Exists(filePath)) { return; } FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate); int streamLength = (int)fs.Length; byte[] buffer = new byte[streamLength]; fs.Read(buffer, 0, streamLength); HttpContext.Current.Response.ContentType = "application/octet-stream"; HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename="+fileName+";"); ////attachment --- 作为附件下载 ////inline --- 在线打开 HttpContext.Current.Response.AddHeader("Content-Length", streamLength.ToString()); HttpContext.Current.Response.BinaryWrite(buffer); fs.Close(); HttpContext.Current.Response.Flush(); HttpContext.Current.Response.End(); } }

 

转载于:https://www.cnblogs.com/xingbinggong/p/3195951.html

你可能感兴趣的文章
学习笔记-模块之xml文件处理
查看>>
接口测试用例
查看>>
面试:用 Java 实现一个 Singleton 模式
查看>>
Sybase IQ导出文件的几种方式
查看>>
案例:手动输入一个字符串,打散放进一个列表,小写字母反序 大写字母保持不变...
查看>>
linux 系统下 tar 的压缩与解压缩命令
查看>>
阿里负载均衡,配置中间证书问题(在starcom申请免费DV ssl)
查看>>
转:How to force a wordbreaker to be used in Sharepoint Search
查看>>
MySQL存储过程定时任务
查看>>
Python中and(逻辑与)计算法则
查看>>
POJ 3267 The Cow Lexicon(动态规划)
查看>>
设计原理+设计模式
查看>>
音视频处理
查看>>
tomcat 7服务器跨域问题解决
查看>>
前台实现ajax 需注意的地方
查看>>
Jenkins安装配置
查看>>
个人工作总结05(第二阶段)
查看>>
Java clone() 浅拷贝 深拷贝
查看>>
深入理解Java虚拟机&运行时数据区
查看>>
02-环境搭建
查看>>