码迷,mamicode.com
首页 > Web开发 > 详细

ASP.NET文件操作通用类

时间:2014-05-29 01:07:43      阅读:392      评论:0      收藏:0      [点我收藏+]

标签:style   c   class   blog   code   java   

bubuko.com,布布扣
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 
  6 using System.IO;
  7 using System.Web;
  8 using System.Web.UI;
  9 using System.Web.UI.WebControls;
 10 
 11 
 12 public class WebFileHelper
 13 {
 14 
 15     FileInfo fileInfo = null;
 16 
 17     HttpResponse Response = System.Web.HttpContext.Current.Response;
 18 
 19 
 20     public WebFileHelper() { }
 21 
 22     public WebFileHelper(HttpResponse response)
 23     {
 24         Response = response;
 25     }
 26 
 27     /// <summary>
 28     /// 判断指定文件是否存在
 29     /// </summary>
 30     /// <param name="fullName">指定文件完整路径</param>
 31     /// <returns>存在返回true;否则返回false</returns>
 32     public bool IsFileExists(string fullName)
 33     {
 34         fileInfo = new FileInfo(fullName);
 35         if (fileInfo.Exists)
 36             return true;
 37         return false;
 38     }
 39 
 40 
 41     /// <summary>
 42     /// 上传文件
 43     /// </summary>
 44     /// <param name="postedFile">上传的文件</param>
 45     /// <param name="saveAsFullName">文件保存的完整路径,但不能重名</param>
 46     /// <returns>成功返回true;否则返回false</returns>
 47     public bool UploadFile(HttpPostedFile postedFile, string saveAsFullName)
 48     {
 49         return UploadFile(postedFile, saveAsFullName, false);
 50     }
 51 
 52     /// <summary>
 53     /// 上传文件
 54     /// </summary>
 55     /// <param name="postedFile">上传的文件</param>
 56     /// <param name="saveAsFullName">文件保存的完整路径</param>
 57     /// <param name="isReplace">如果有同名文件存在,是否覆盖</param>
 58     /// <returns>成功返回true,否则返回false</returns>
 59     public bool UploadFile(HttpPostedFile postedFile, string saveAsFullName, bool isReplace)
 60     {
 61         try
 62         {
 63             if (!isReplace && IsFileExists(saveAsFullName))
 64                 return false;
 65             postedFile.SaveAs(saveAsFullName);
 66             return true;
 67         }
 68         catch (Exception ex)
 69         {
 70             throw ex;
 71         }
 72     }
 73 
 74 
 75     /// <summary>
 76     /// 文件下载
 77     /// </summary>
 78     /// <param name="fullName">文件完整路径</param>
 79     /// <returns>下载成功返回true,否则返回false</returns>
 80     public bool DownloadFile(string fullName)
 81     {
 82         return DownloadFile(fullName, fullName.Substring(fullName.LastIndexOf(@"\") + 1));
 83     }
 84 
 85     /// <summary>
 86     /// 文件下载
 87     /// </summary>
 88     /// <param name="fullName">文件完整路径</param>
 89     /// <param name="sendFileName">发送到客户端显示的文件名</param>
 90     /// <returns>下载成功返回true,否则返回false</returns>
 91     public bool DownloadFile(string fullName, string sendFileName)
 92     {
 93         try
 94         {
 95             fileInfo = new FileInfo(fullName);
 96             Response.Clear();
 97             Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(sendFileName, System.Text.Encoding.UTF8));
 98             Response.AddHeader("Content-Length", fileInfo.Length.ToString());
 99             Response.ContentType = "application/octet-stream";
100             Response.WriteFile(fileInfo.FullName);
101             Response.End();
102             Response.Flush();
103             Response.Clear();
104             return true;
105         }
106         catch (Exception ex)
107         {
108             throw ex;
109         }
110     }
111 
112     /// <summary>
113     /// 删除文件
114     /// </summary>
115     /// <param name="fullName">文件完整路径</param>
116     /// <returns>删除成功返回true,否则返回false</returns>
117     public bool DeleteFile(string fullName)
118     {
119         if (!IsFileExists(fullName))
120             return false;
121         try
122         {
123             fileInfo = new FileInfo(fullName);
124             fileInfo.Delete();
125             return true;
126         }
127         catch (Exception ex)
128         {
129             throw ex;
130         }
131     }
132 
133 
134     /// <summary>
135     /// 移动文件
136     /// </summary>
137     /// <param name="fullName">文件完整路径</param>
138     /// <param name="newFullName">文件移动到的完整路径(可重新命名,但不能重名)</param>
139     /// <returns>移动成功返回true,否则返回false</returns>
140     public bool MoveTo(string fullName, string newFullName)
141     {
142         return MoveTo(fullName, newFullName, false);
143     }
144 
145     /// <summary>
146     /// 移动文件
147     /// </summary>
148     /// <param name="fullName">文件完整路径</param>
149     /// <param name="newFullName">文件移动到的完整路径(可重新命名)</param>
150     /// <param name="isReplace">如果有同名文件存在,是否覆盖</param>
151     /// <returns>移动成功返回true,否则返回false</returns>
152     public bool MoveTo(string fullName, string newFullName, bool isReplace)
153     {
154         if (!isReplace && IsFileExists(fullName))
155             return false;
156         try
157         {
158             fileInfo = new FileInfo(fullName);
159             fileInfo.MoveTo(newFullName);
160             return true;
161         }
162         catch (Exception ex)
163         {
164             throw ex;
165         }
166     }
167 
168     /// <summary>
169     /// 复制文件
170     /// </summary>
171     /// <param name="fullName">文件完整路径</param>
172     /// <param name="newFullName">文件移动到的完整路径(可重新命名)</param>
173     /// <returns>复制成功返回true,否则返回false</returns>
174     public bool CopyTo(string fullName, string newFullName)
175     {
176         return CopyTo(fullName, newFullName, false);
177     }
178 
179     /// <summary>
180     /// 复制文件
181     /// </summary>
182     /// <param name="fullName">文件完整路径</param>
183     /// <param name="newFullName">文件移动到的完整路径(可重新命名)</param>
184     /// <param name="isReplace">如果有同名文件存在,是否覆盖</param>
185     /// <returns>复制成功返回true,否则返回false</returns>
186     public bool CopyTo(string fullName, string newFullName, bool isReplace)
187     {
188         if (!isReplace && IsFileExists(fullName))
189             return false;
190         try
191         {
192             fileInfo = new FileInfo(fullName);
193             fileInfo.CopyTo(newFullName, false);
194             return true;
195         }
196         catch (Exception ex)
197         {
198             throw ex;
199         }
200     }
201 }
bubuko.com,布布扣

 

ASP.NET文件操作通用类,布布扣,bubuko.com

ASP.NET文件操作通用类

标签:style   c   class   blog   code   java   

原文地址:http://www.cnblogs.com/zengvip/p/3754485.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!