Asp.net实现附件下载(乱码解决)

代码
private void Page_Load(object sender, System.EventArgs e)
{
if (Request.QueryString["Recordid"] != null && Request.QueryString["Recordid"].ToString() != "")
{
StarTech.DBUtility.AdoHelper adoHelper
= StarTech.DBUtility.AdoHelper.CreateHelper("News_Instance");
System.Data.DataTable resourceDs
= adoHelper.ExecuteSqlDataset("Select * From t_news_resource Where Recordid='" + Request.QueryString["Recordid"].ToString() + "'").Tables[0];
if (resourceDs.Rows.Count > 0)
{
string filepath = this.MapPath(resourceDs.Rows[0]["path"].ToString());
byte[] ByteFile = GetFileByte(filepath);
if (ByteFile != null)
{
string FileType = System.IO.Path.GetExtension(filepath);
string FileName = System.IO.Path.GetFileName(filepath);
Response.Clear();
Response.HeaderEncoding
= System.Text.Encoding.Default;
Response.AddHeader(
"Content-Disposition", "attachment; filename=" + FileName);
Response.AddHeader(
"Content-Length", ByteFile.Length.ToString());
Response.ContentType
= CheckType(FileType);
Response.BinaryWrite(ByteFile);
Response.End();
}
else
{
Response.Write(
"<font color=red>找不到相关附件!【<a href='javascript:window.opener = null;window.close()'>关闭</a>】</font>");
}
}
}
else
{
Response.Write(
"<font color=red>参数错误或者权限不够 【<a href='javascript:window.close()'>关闭</a>】</font>");
}
}
//获取文件二进制流
private Byte[] GetFileByte(string path)
{
try
{
System.IO.FileStream file
= new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read);
Byte[] imgByte
= new Byte[file.Length];
file.Read(imgByte,
0, imgByte.Length);
file.Close();
return imgByte;
}
catch (Exception ex)
{
string str = ex.Message;
return null;
}
}
/// <summary>
/// 根据文件的扩展名来获取对应的"输出流的HTTP MIME"类型
/// </summary>
/// <param name="FileType"></param>
/// <returns></returns>
private string CheckType(string FileType)
{
string ContentType;
switch (FileType.ToLower())
{
case ".asf ":
ContentType
= "video/x-ms-asf ";
break;
case ".avi ":
ContentType
= "video/avi ";
break;
case ".doc ":
ContentType
= "application/msword "; break;
case ".zip ":
ContentType
= "application/zip "; break;
case ".xls ":
ContentType
= "application/vnd.ms-excel "; break;
case ".gif ":
ContentType
= "image/gif "; break;
case ".jpg ":
ContentType
= "image/jpeg "; break;
case "jpeg ":
ContentType
= "image/jpeg "; break;
case ".wav ":
ContentType
= "audio/wav "; break;
case ".mp3 ":
ContentType
= "audio/mpeg3 "; break;
case ".mpg ":
ContentType
= "video/mpeg "; break;
case ".mepg ":
ContentType
= "video/mpeg "; break;
case ".rtf ":
ContentType
= "application/rtf "; break;
case ".html ":
ContentType
= "text/html "; break;
case ".htm ":
ContentType
= "text/html "; break;
case ".txt ":
ContentType
= "text/plain "; break;
default:
ContentType
= "application/octet-stream ";
break;
}
return ContentType;
}