using System;
using System.Text;
using System.Collections.Generic;
namespace RongPan.TuDou.Common
{
public class FileHelp
{
public static void DownloadFile(System.Web.HttpResponse response,string filePath)
{
response.Clear();
response.ContentType = "application/x-zip-compressed";
response.TransmitFile(filePath);
response.End();
}
///
/// 上传文件并获得服务器上的文件名
///
///
///
///
public static string UpLoadFile(System.Web.UI.HtmlControls.HtmlInputFile inputFile, string strAbsolutePath)
{
string strOldFilePath = inputFile.PostedFile.FileName;
string strExtension = string.Empty;
string strNewFilePath = string.Empty;
if (strOldFilePath != null)
{
strNewFilePath = GetNewFileName(strOldFilePath);
try
{
inputFile.PostedFile.SaveAs(GetSavePath(strNewFilePath, strAbsolutePath));
}
catch
{
throw new Exception(string.Format("{0}文件上传失败", strOldFilePath));
}
}
else
{
throw new Exception("文件不存在,无法上传!");
}
return strNewFilePath;
}
///
/// 多文件上传
///
///
///
///
public static List
{
if (files.Count > 0)
{
List
foreach (System.Web.UI.HtmlControls.HtmlInputFile item in files)
{
string oldFileName = item.PostedFile.FileName;
oldFileName = oldFileName.Substring(oldFileName.LastIndexOf('//'));
string fileName = string.Empty;
try
{
fileName = UpLoadFile(item, strAbsolutePath);
}
catch (Exception e)
{
throw e;
}
finally
{
if (list.Count>0)
{
foreach (Filer temp in list)
{
DeleteFile(temp.AbsolutePath);
}
}
}
string absolutePath = GetSavePath(fileName, strAbsolutePath);
list.Add(new Filer(fileName, absolutePath, oldFileName));
}
return list;
}
else
{
throw new Exception("没有找到将要上传的文件!");
}
}
public static List
{
return UpLoadFile(request.Files, strAbsolutePath);
}
public static List
{
return UpLoadFile(context.Request.Files, strAbsolutePath);
}
///
/// 删除指定的文件
///
/// 文件不存在不会引发异常
public static void DeleteFile(string absolutePath)
{
System.IO.File.Delete(absolutePath);
}
///
/// 删除指定的文件
///
/// 文件不存在不会引发异常
/// 文件不存在不会引发异常
public static void DeleteFile(string fileName, string absolutepath)
{
DeleteFile(GetSavePath(fileName,absolutepath));
}
///
/// 获得文件名的扩展名
///
///
///
public static string GetPathStringExtension(string pathString)
{
if (pathString.Length > 0)
{
return pathString.Substring(pathString.LastIndexOf('.'));
}
else
{
throw new Exception("文件名为空,无法获取其扩展名!");
}
}
///
/// 拼接文件绝对路径
///
///
///
///
public static string GetSavePath(string fileName, string absolutePath)
{
string savePath = string.Empty;
if (absolutePath.LastIndexOf('//') == absolutePath.Length)
{
savePath = absolutePath + fileName;
}
else
{
savePath = absolutePath + "//" + fileName;
}
return savePath;
}
///
/// 获得一个唯一的文件名
///
///
///
public static string GetNewFileName(string fileName)
{
string newFileName = string.Empty;
string extension = GetPathStringExtension(fileName);
return Code.GetGUID() + extension;
}
///
/// 修改上传的文件
///
///
///
///
///
public static string CoverFile(System.Web.UI.HtmlControls.HtmlInputFile ffFile,string strAbsolutePath,string strOldFileName)
{
if (ffFile.PostedFile.FileName != string.Empty)
{
if (strOldFileName != string.Empty)
{
DeleteFile(strOldFileName, strAbsolutePath);
}
return UpLoadFile(ffFile, strAbsolutePath);
}
else
{
throw new Exception("文件不存在,无法上传!");
}
}
///
/// 批量修改
///
///
///
///
///
public static List
{
if (files.Count > 0)
{
List
foreach (Filer item in oldFilers)
{
DeleteFile(item.AbsolutePath);
}
return list;
}
else
{
throw new Exception("没有找到将要上传的文件!");
}
}
public static List
{
return CoverFile(request.Files,absolutePath,oldFilers);
}
public static List
{
return CoverFile(context.Request.Files,ablsolutePath,oldFilers);
}
}
}
Code类,用来实现加密、获取全局唯一键等功能。
在上面的例子中可能用到。
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
namespace RongPan.TuDou.Common
{
public class Code
{
///
/// 获得字符串的MD5编码,速度快
///
/// 需要加密的字符串
///
public static string GetMd5HashCode(string input)
{
//
// Create a new instance of the MD5CryptoServiceProvider object.
MD5 md5Hasher = MD5.Create();
// Convert the input string to a byte array and compute the hash.
byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
// Create a new Stringbuilder to collect the bytes
// and create a string.
StringBuilder sBuilder = new StringBuilder();
// Loop through each byte of the hashed data
// and format each one as a hexadecimal string.
foreach (byte b in data)
{
sBuilder.Append(b.ToString("x2"));
}
// Return the hexadecimal string.
return sBuilder.ToString();
}
///
/// 获得字符串的SHA1编码,更安全
///
/// 需要加密的字符串
///
public static string GetSHA1HashCode(string input)
{
//
byte[] data = Encoding.Default.GetBytes(input);
byte[] result;
SHA1 sha = new SHA1CryptoServiceProvider();
// This is one implementation of the abstract class SHA1.
result = sha.ComputeHash(data);
StringBuilder sBuilder = new StringBuilder();
foreach(byte b in result)
{
sBuilder.Append(b.ToString("x2"));
}
return sBuilder.ToString();
}
///
/// 获得全局唯一标识
///
///
public static string GetGUID()
{
return Guid.NewGuid().ToString().Replace('-', '/0');
}
///
/// 编码
///
///
///
public static string DoCode(string var)
{
string code = string.Empty;
//进行编码
return code;
}
///
/// 反编码
///
///
///
public static string DeCode(string code)
{
string var = string.Empty;
//进行反编码
return var;
}
///
/// 生成指定长度英文字母
///
///
///
public static string NextCheckCode(int codeCount)
{
string[] allChar = new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", " K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "W", " X", "Y", "Z " };
Random rnd = new Random((int)DateTime.Now.Ticks);
StringBuilder code = new StringBuilder();
for (int i = 0; i < codeCount; i++)
{
code.Append(allChar[rnd.Next(0, 25)]);
}
return code.ToString().Replace(" ","");
}
///
/// 生成5位随机英文字母
///
///
public static string NextCheckCode()
{
return NextCheckCode(5);
}
}
}