您的位置:首页 > 编程语言 > ASP

很不错的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]));
}
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: