文件操作帮助类

本文介绍了一个文件操作帮助类,该类提供了文件上传、下载、删除等功能,并详细展示了如何实现这些功能的代码。通过这个帮助类可以方便地进行文件管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文件操作帮助类:

    /// <summary>
    /// 对文件上传操作的帮助类
    /// </summary>
    public class FileManager
    {
        /// <summary>
        /// 上传文件目录名称
        /// </summary>
        private static string FileFolder = ConfigurationSettings.AppSettings["UploadFolder"];

        /// <summary>
        /// 上传文件基础目录
        /// </summary>
        private static string BaseFolder = HttpContext.Current.Server.MapPath("~/"+FileFolder);

        /// <summary>
        /// 根网站
        /// </summary>
        private static string RootSite = ConfigurationSettings.AppSettings["RootSite"];

        /// <summary>
        /// 文件基础路径
        /// </summary>
        public static string FileBaseUrl = RootSite + "/" + FileFolder + "/";

        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="file"></param>
        /// <returns>文件上传后的url</returns>
        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;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileFullName"></param>
        /// <param name="file"></param>
        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();
            }
        }


        /// <summary>
        /// 创建对象
        /// </summary>
        /// <param name="folderName"></param>
        public static void CreateFile(string folderName)
        {
            DirectoryInfo di = new DirectoryInfo(folderName);
            if (!di.Exists)
            {
                di.Create();
            }
        }



        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="fileRelativePath">UploadFile返回的字符串</param>
        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();
            }
        }

        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="fileRelativePath">文件相对路径</param>
        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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值