ASP.NET 2.0 文件下载功能详解(解决了文件名乱码的情形)
来源:百度文库 编辑:神马文学网 时间:2024/04/29 08:45:27
1.前台设计 三个Button控件,部分代码如下
2.后台设计(该代码解决了文件中文名出现乱码的情况)
1)添加命名空间 using System.IO;
2)分别添加每个按钮的单击事件,实现三种不同方式的下载
3)后台代码如下
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
namespace WebTest
{
public partial class DownLoad : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.ContentType = "audio/x-mpeg";
string strtmp = System.Web.HttpUtility.UrlEncode("爱情木瓜.mp3", System.Text.Encoding.UTF8);
Response.AddHeader("Content-Disposition", "attachment;filename="+strtmp);
string filename = Server.MapPath("DownLoad/爱情木瓜.mp3");
Response.TransmitFile(filename);
}
protected void Button2_Click(object sender, EventArgs e)
{
string fileName = "爱情木瓜.mp3";
string filePath = Server.MapPath("DownLoad/爱情木瓜.mp3");//DownLoad是你网站根目录下的文件夹
FileInfo fileInfo = new FileInfo(filePath);
Response.Clear(); //清除服务器中缓冲的输出
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));//解决中文乱码
Response.AddHeader("Content-Length", fileInfo.Length.ToString());
Response.AddHeader("Content-Transfer-Encoding", "binary");
Response.ContentType = "audio/x-mpeg";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
Response.WriteFile(fileInfo.FullName);
Response.Flush(); //立即发送缓冲的输出
Response.End();
}
protected void Button3_Click(object sender, EventArgs e)
{
string fileName = "爱情木瓜.mp3";
string filePath = Server.MapPath("DownLoad/爱情木瓜.mp3");
FileInfo fi = new FileInfo(filePath);
if (fi.Exists == true)
{
const long ChunkSize = 102400;
byte[] buffer = new byte[ChunkSize];
Response.Clear();
System.IO.FileStream istream = System.IO.File.OpenRead(filePath);
long Data = istream.Length;
Response.ContentType = "audio/x-mpeg";
Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName));
while (Data > 0 && Response.IsClientConnected)
{
int lengthread = istream.Read(buffer, 0, Convert.ToInt32(ChunkSize));
Response.OutputStream.Write(buffer, 0, lengthread);
Response.Flush();
Data = Data - lengthread;
}
Response.Close();
}
文件下载 | ||||
2.后台设计(该代码解决了文件中文名出现乱码的情况)
1)添加命名空间 using System.IO;
2)分别添加每个按钮的单击事件,实现三种不同方式的下载
3)后台代码如下
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
namespace WebTest
{
public partial class DownLoad : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.ContentType = "audio/x-mpeg";
string strtmp = System.Web.HttpUtility.UrlEncode("爱情木瓜.mp3", System.Text.Encoding.UTF8);
Response.AddHeader("Content-Disposition", "attachment;filename="+strtmp);
string filename = Server.MapPath("DownLoad/爱情木瓜.mp3");
Response.TransmitFile(filename);
}
protected void Button2_Click(object sender, EventArgs e)
{
string fileName = "爱情木瓜.mp3";
string filePath = Server.MapPath("DownLoad/爱情木瓜.mp3");//DownLoad是你网站根目录下的文件夹
FileInfo fileInfo = new FileInfo(filePath);
Response.Clear(); //清除服务器中缓冲的输出
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));//解决中文乱码
Response.AddHeader("Content-Length", fileInfo.Length.ToString());
Response.AddHeader("Content-Transfer-Encoding", "binary");
Response.ContentType = "audio/x-mpeg";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
Response.WriteFile(fileInfo.FullName);
Response.Flush(); //立即发送缓冲的输出
Response.End();
}
protected void Button3_Click(object sender, EventArgs e)
{
string fileName = "爱情木瓜.mp3";
string filePath = Server.MapPath("DownLoad/爱情木瓜.mp3");
FileInfo fi = new FileInfo(filePath);
if (fi.Exists == true)
{
const long ChunkSize = 102400;
byte[] buffer = new byte[ChunkSize];
Response.Clear();
System.IO.FileStream istream = System.IO.File.OpenRead(filePath);
long Data = istream.Length;
Response.ContentType = "audio/x-mpeg";
Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName));
while (Data > 0 && Response.IsClientConnected)
{
int lengthread = istream.Read(buffer, 0, Convert.ToInt32(ChunkSize));
Response.OutputStream.Write(buffer, 0, lengthread);
Response.Flush();
Data = Data - lengthread;
}
Response.Close();
}
ASP.NET 2.0 文件下载功能详解(解决了文件名乱码的情形)
跨平台(Linux&Windows)中文文件名乱码的解决 - linux - QT开发爱好者
ASP.net中上传文件的操作
ASP.net中上传文件的操作
ASP.NET 2.0数据缓存功能简介
用ASP实现论坛的UBB功能->>网页教学网(http://www.webjx.com)建站指南|网页制作|网页特效|Flash动画|网络编程|素材下载|教程下载|建站|HTML|CSS|JAVASCRIPT|ASP|PHP|JSP|ASP.NET|数据库
ASP.NET验证控件详解
ASP.NET中web.config文件的配置
ASP.NET中web.config文件的配置
在ASP.NET中设置html文件的访问验证
ASP.NET 2.0 中的 Theme 功能。 - MaxFree's Tech Sp...
ASP.NET_ASP.NET 2.0数据缓存功能简介
关于ASP.net服务器的入侵方法详解
ASP实现文件直接下载
ASP实现文件直接下载
ASP.NET 4新增功能(四) 其它新功能及VS 2010对ASP.NET 4的支持改进...
解决Linux乱码的方法
使用ASP.NET Global.asax 文件
ASP.NET 2.0中XSLT的使用
ASP.NET 2.0中XSLT的使用
asp.net的生命周期
Asp.Net文件上传之简单文件上传(上) - ASP.net
终于解决了Apache乱码问题
asp与asp.net的区别