注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

天涯倦客的博客

祝福你朋友永远快乐!

 
 
 

日志

 
 

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

2011-12-14 17:58:08|  分类: asp.net |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

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

Asp.net实现附件下载(乱码解决) - 海里的贝壳 - apple的博客代码
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;
}
复制代码

 

———————————————————————————————————
浩瀚的天空,会有改变的希望,世界会不会变得更加好,选择在於我们的手上。
———————————————————————————————————
  评论这张
 
阅读(605)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017