很不错的asp.net文件上传类c# 搜索文件 移动文件 删除文件等
2016-12-29 00:00
246 查看
using System; using System.Collections.Generic; using System.Text; using System.Data.SqlClient; using System.IO; namespace Tool { /// <summary> /// file operate class /// </summary> public class FileUpload { /// <summary> /// Upload file /// </summary> /// <param name="path">The path of folder you want to put the file</param> /// <param name="UpFile">A entity of class HtmlInputFile</param> /// <param name="docType">Allowed file type eg:'PNG,JPG,TIF,GIF,BMP'</param> /// <returns>Result of upload</returns> //public string Upload(string path,System.Web.UI.HtmlControls.HtmlInputFile UpFile,string docType) public string Upload(string path, System.Web.UI.WebControls.FileUpload UpFile, string docType) { string modifyFileName = DateTime.Now.Year.ToString(); modifyFileName += DateTime.Now.Month.ToString(); modifyFileName += DateTime.Now.Day.ToString(); modifyFileName += DateTime.Now.Hour.ToString(); modifyFileName += DateTime.Now.Minute.ToString(); modifyFileName += DateTime.Now.Second.ToString(); modifyFileName += DateTime.Now.Millisecond.ToString(); string value = UpFile.FileName; string tFileType = value.Substring(value.LastIndexOf(".") + 1); string[] allowedType = docType.Split(','); bool TypeMatched = false; if (value == "" || value == null) { return "-1"; //Error:Uploaded file is null! } else { int FileLength = UpFile.PostedFile.ContentLength; if (FileLength > (20 * 1024 * 1024)) { return "-2"; //Error:Uploaded file is too large! } foreach (string type in allowedType) { if (type.ToLower().IndexOf(tFileType) != -1) { TypeMatched = true; } } if (!TypeMatched) { return "-3"; //Error:Wrong type of Uploaded file! } string dirPath = System.Web.HttpContext.Current.Server.MapPath(path); System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(dirPath); if (!dir.Exists) { dir.Create(); } path = path + modifyFileName + "." + tFileType; UpFile.PostedFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath(path)); return System.Web.HttpContext.Current.Server.MapPath(path); //Upload succeed! } } /// <summary> /// Search a file from the mapped path /// </summary> /// <param name="path">The mappsed path</param> /// <param name="fileName">The file name for search</param> /// <returns>Result of search</returns> public int SearchFile(string path, string fileName) { System.IO.DirectoryInfo SearchDir = new System.IO.DirectoryInfo(System.Web.HttpContext.Current.Server.MapPath(path)); foreach (System.IO.FileInfo File in SearchDir.GetFiles()) { if (File.Name.IndexOf(fileName) != -1) { return 1;// Find } } return 0;//Not find } /// <summary> /// Delete a file /// </summary> /// <param name="path">The path of file</param> /// <returns>The result of delete</returns> public int DeleteFile(string path) { System.IO.FileInfo File = new System.IO.FileInfo(System.Web.HttpContext.Current.Server.MapPath(path)); if (File.Exists) { if (File.IsReadOnly) { return -1;//The file is readonly } else { File.Delete(); return 1;//Delete succeed } } else { return 0;//The file is not exsits } } /// <summary> /// Move a file /// </summary> /// <param name="sFolder">source folder</param> /// <param name="sName">source name</param> /// <param name="tFolder">object folder</param> /// <param name="tName">object name</param> /// <returns></returns> public int FileMove(string sFolder, string sName, string tFolder, string tName) { string[] arrDirs = Directory.GetDirectories(sFolder); string[] arrFiles = Directory.GetFiles(sFolder); if (arrFiles.Length != 0) { //copy this file File.Copy(sFolder + "//" + sName, tFolder + "//" + tName, true); //delete the old file return DeleteFile(sFolder + "//" + sName); } else { return -1; } } /// <summary> /// Move selected files /// </summary> /// <param name="sFolder">source folder</param> /// <param name="tFolder">object folder</param> public void FileMove(string sFolder, string tFolder) { string[] arrDirs = Directory.GetDirectories(sFolder); string[] arrFiles = Directory.GetFiles(sFolder); if (arrFiles.Length != 0) { for (int i = 0; i < arrFiles.Length; i++) { //copy these files File.Copy(sFolder + "//" + Path.GetFileName(arrFiles[i]), tFolder + "//" + Path.GetFileName(arrFiles[i]), true); //delete old files DeleteFile(sFolder + "//" + Path.GetFileName(arrFiles[i])); } } } } }
相关文章推荐
- asp.net调用Lodop实现页面打印或局部打印,可进行打印设置或预览
- Asp.net采用替换模版页的形式生成静态页
- asp.net中Execl文件的导入导出
- asp.net mvc5中使用缓存依赖SqlCacheDependency
- asp.net利用FCK编辑器中的分页符 完成长文章分页
- asp.net(C#)生成Code39条形码 条码枪可以扫描出
- Asp.net(c#)常用文件操作类封装 移动 复制 删除 上传 下载等
- Asp.Net MVC4 系列--进阶篇之路由 (2)
- 一步使你的asp.net网站在手机浏览器上全屏显示
- asp.net数据四舍五入
- ASP.NET将Session保存到数据库中
- asp.net中kindeditor配置
- ASP.NET MVC AJAX的调用示例
- asp.net中自动填充参数的存储过程类(C#)
- 比较完善的asp.net2.0邮件发送类
- ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之目录导航
- 树莓派3配置无线热点
- C# Aspose word 替换指定键值数据
- 比较分析 Spring AOP 和 AspectJ 之间的差别 【已翻译100%】
- Java 8的元空间(metaspace)