ASP.NET上传下载文件,使用TransmitFile方法实现下载。

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();
}
/// <summary>
/// 上传文件并获得服务器上的文件名
/// </summary>
/// <param name="inputFile"></param>
/// <param name="strAbsolutePath"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 多文件上传
/// </summary>
/// <param name="files"></param>
/// <param name="strAbsolutePath"></param>
/// <returns></returns>
public static List<Filer> UpLoadFile(System.Web.HttpFileCollection files, string strAbsolutePath)
{
if (files.Count > 0)
{
List<Filer> list = new List<Filer>();
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<Filer> UpLoadFile(System.Web.HttpRequest request, string strAbsolutePath)
{
return UpLoadFile(request.Files, strAbsolutePath);
}
public static List<Filer> UpLoadFile(System.Web.HttpContext context, string strAbsolutePath)
{
return UpLoadFile(context.Request.Files, strAbsolutePath);
}
/// <summary>
/// 删除指定的文件
/// </summary>
/// <param name="absolutePath">文件不存在不会引发异常</param>
public static void DeleteFile(string absolutePath)
{
System.IO.File.Delete(absolutePath);
}
/// <summary>
/// 删除指定的文件
/// </summary>
/// <param name="fileName">文件不存在不会引发异常</param>
/// <param name="absolutepath">文件不存在不会引发异常</param>
public static void DeleteFile(string fileName, string absolutepath)
{
DeleteFile(GetSavePath(fileName,absolutepath));
}
/// <summary>
/// 获得文件名的扩展名
/// </summary>
/// <param name="pathString"></param>
/// <returns></returns>
public static string GetPathStringExtension(string pathString)
{
if (pathString.Length > 0)
{
return pathString.Substring(pathString.LastIndexOf('.'));
}
else
{
throw new Exception("文件名为空,无法获取其扩展名!");
}
}
/// <summary>
/// 拼接文件绝对路径
/// </summary>
/// <param name="fileName"></param>
/// <param name="absolutePath"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 获得一个唯一的文件名
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public static string GetNewFileName(string fileName)
{
string newFileName = string.Empty;
string extension = GetPathStringExtension(fileName);
return Code.GetGUID() + extension;

}
/// <summary>
/// 修改上传的文件
/// </summary>
/// <param name="ffFile"></param>
/// <param name="strAbsolutePath"></param>
/// <param name="strOldFileName"></param>
/// <returns></returns>
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("文件不存在,无法上传!");
}
}
/// <summary>
/// 批量修改
/// </summary>
/// <param name="files"></param>
/// <param name="strAbsolutePath"></param>
/// <param name="oldFilers"></param>
/// <returns></returns>
public static List<Filer> CoverFile(System.Web.HttpFileCollection files, string strAbsolutePath, List<Filer> oldFilers)
{
if (files.Count > 0)
{
List<Filer> list = UpLoadFile(files, strAbsolutePath);
foreach (Filer item in oldFilers)
{
DeleteFile(item.AbsolutePath);
}
return list;
}
else
{
throw new Exception("没有找到将要上传的文件!");
}
}
public static List<Filer> CoverFile(System.Web.HttpRequest request, string absolutePath, List<Filer> oldFilers)
{
return CoverFile(request.Files,absolutePath,oldFilers);
}
public static List<Filer> CoverFile(System.Web.HttpContext context, string ablsolutePath, List<Filer> oldFilers)
{
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
{
/// <summary>
/// 获得字符串的MD5编码,速度快
/// </summary>
/// <param name="input">需要加密的字符串</param>
/// <returns>MD5 Code</returns>
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();
}
/// <summary>
/// 获得字符串的SHA1编码,更安全
/// </summary>
/// <param name="input">需要加密的字符串</param>
/// <returns>SHA1 Code</returns>
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();
}
/// <summary>
/// 获得全局唯一标识
/// </summary>
/// <returns></returns>
public static string GetGUID()
{
return Guid.NewGuid().ToString().Replace('-', '/0');
}
/// <summary>
/// 编码
/// </summary>
/// <param name="var"></param>
/// <returns></returns>
public static string DoCode(string var)
{
string code = string.Empty;
//进行编码
return code;
}
/// <summary>
/// 反编码
/// </summary>
/// <param name="code"></param>
/// <returns></returns>
public static string DeCode(string code)
{
string var = string.Empty;
//进行反编码
return var;
}
/// <summary>
/// 生成指定长度英文字母
/// </summary>
/// <param name="codeCount"></param>
/// <returns></returns>
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(" ","");
}
/// <summary>
/// 生成5位随机英文字母
/// </summary>
/// <returns></returns>
public static string NextCheckCode()
{
return NextCheckCode(5);
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值