文件路径是不是有中文啊?zh-tw 这是繁体的!
asp.net2.0以后,我们有了很多种文件下载的选择。
public partial class FileDownLoad : System.Web.UI.Page
{
//提供下载的文件,不编码的话文件名会乱码
private string fileName = HttpContext.Current.Server.UrlEncode("规范.rar");
private string filePath = HttpContext.Current.Server.MapPath("规范.rar");
//使用TransmifFile下载文件
protected void btnDL1_Click(object sender, EventArgs e)
{
FileInfo info = new FileInfo(filePath);
long fileSize = info.Length;
Response.Clear();
Response.ContentType = "application/x-zip-compressed";
Response.AddHeader("Content-Disposition", "attachment;filename="+ fileName);
//不指明Content-Length用Flush的话不会显示下载进度
Response.AddHeader("Content-Length", fileSize.ToString());
Response.TransmitFile(filePath, 0, fileSize);
Response.Flush();
Response.Close();
}
//使用WriteFile下载文件
protected void btnDL2_Click(object sender, EventArgs e)
{
FileInfo info = new FileInfo(filePath);
long fileSize = info.Length;
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachement;filename=" + fileName);
//指定文件大小
Response.AddHeader("Content-Length", fileSize.ToString());
Response.WriteFile(filePath, 0, fileSize);
Response.Flush();
Response.Close();
}
//使用OutputStream.Write分块下载文件
protected void btnDL3_Click(object sender, EventArgs e)
{
//指定块大小
long chunkSize = 102400;
//建立一个100K的缓冲区
byte[] buffer = new byte[chunkSize];
//已读的字节数
long dataToRead = 0;
FileStream stream = null;
try
{
//打开文件
stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
dataToRead = stream.Length;
//添加Http头
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachement;filename=" + fileName);
Response.AddHeader("Content-Length", dataToRead.ToString());
while (dataToRead > 0)
{
if (Response.IsClientConnected)
{
int length = stream.Read(buffer, 0, Convert.ToInt32(chunkSize));
Response.OutputStream.Write(buffer, 0, length);
Response.Flush();
Response.Clear();
dataToRead -= length;
}
else
{
//防止client失去连接
dataToRead = -1;
}
}
}
catch (Exception ex)
{
Response.Write("Error:" + ex.Message);
}
finally
{
if (stream != null)
{
stream.Close();
}
Response.Close();
}
}
//使用BinaryWrite下载文件,大文件效率不行
protected void btnDL4_Click(object sender, EventArgs e)
{
FileStream stream = null;
try
{
//读文件,大文件一次读入会占用大量内存
stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
stream.Close();
//添加Http头
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachement;filename=" + fileName);
Response.AddHeader("Content-Length", bytes.Length.ToString());
Response.BinaryWrite(bytes);
Response.Flush();
}
catch (Exception ex)
{
Response.Write("Error:" + ex.Message);
}
finally
{
if (stream != null)
{
stream.Close();
}
Response.Close();
}
}
//使用BinaryWrite分块下载文件
protected void btnDL5_Click(object sender, EventArgs e)
{
//指定区块和缓冲区
long chunkSize = 102400;
byte[] buffer = new byte[chunkSize];
FileStream stream = null;
long dataToRead = 0;
try
{
stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
dataToRead = stream.Length;
//添加Http头
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachement;filename=" + fileName);
Response.AddHeader("Content-Length", dataToRead.ToString());
while (dataToRead > 0)
{
if (Response.IsClientConnected)
{
int length = stream.Read(buffer, 0, Convert.ToInt32(chunkSize));
Response.BinaryWrite(buffer);
Response.Flush();
Response.Clear();
dataToRead -= length;
}
else
{
dataToRead = -1;
}
}
}
catch(Exception ex)
{
Response.Write("Error:" + ex.Message);
}
finally
{
if (stream != null)
{
stream.Close();
}
Response.Close();
}
}
}
public partial class FileDownLoad : System.Web.UI.Page { //提供下载的文件,不编码的话文件名会乱码 private string fileName = HttpContext.Current.Server.UrlEncode("规范.rar"); private string filePath = HttpContext.Current.Server.MapPath("规范.rar"); //使用TransmifFile下载文件 protected void btnDL1_Click(object sender, EventArgs e) { FileInfo info = new FileInfo(filePath); long fileSize = info.Length; Response.Clear(); Response.ContentType = "application/x-zip-compressed"; Response.AddHeader("Content-Disposition", "attachment;filename="+ fileName); //不指明Content-Length用Flush的话不会显示下载进度 Response.AddHeader("Content-Length", fileSize.ToString()); Response.TransmitFile(filePath, 0, fileSize); Response.Flush(); Response.Close(); } //使用WriteFile下载文件 protected void btnDL2_Click(object sender, EventArgs e) { FileInfo info = new FileInfo(filePath); long fileSize = info.Length; Response.Clear(); Response.ContentType = "application/octet-stream"; Response.AddHeader("Content-Disposition", "attachement;filename=" + fileName); //指定文件大小 Response.AddHeader("Content-Length", fileSize.ToString()); Response.WriteFile(filePath, 0, fileSize); Response.Flush(); Response.Close(); } //使用OutputStream.Write分块下载文件 protected void btnDL3_Click(object sender, EventArgs e) { //指定块大小 long chunkSize = 102400; //建立一个100K的缓冲区 byte[] buffer = new byte[chunkSize]; //已读的字节数 long dataToRead = 0; FileStream stream = null; try { //打开文件 stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read); dataToRead = stream.Length; //添加Http头 Response.ContentType = "application/octet-stream"; Response.AddHeader("Content-Disposition", "attachement;filename=" + fileName); Response.AddHeader("Content-Length", dataToRead.ToString()); while (dataToRead > 0) { if (Response.IsClientConnected) { int length = stream.Read(buffer, 0, Convert.ToInt32(chunkSize)); Response.OutputStream.Write(buffer, 0, length); Response.Flush(); Response.Clear(); dataToRead -= length; } else { //防止client失去连接 dataToRead = -1; } } } catch (Exception ex) { Response.Write("Error:" + ex.Message); } finally { if (stream != null) { stream.Close(); } Response.Close(); } } //使用BinaryWrite下载文件,大文件效率不行 protected void btnDL4_Click(object sender, EventArgs e) { FileStream stream = null; try { //读文件,大文件一次读入会占用大量内存 stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read); byte[] bytes = new byte[stream.Length]; stream.Read(bytes, 0, bytes.Length); stream.Close(); //添加Http头 Response.ContentType = "application/octet-stream"; Response.AddHeader("Content-Disposition", "attachement;filename=" + fileName); Response.AddHeader("Content-Length", bytes.Length.ToString()); Response.BinaryWrite(bytes); Response.Flush(); } catch (Exception ex) { Response.Write("Error:" + ex.Message); } finally { if (stream != null) { stream.Close(); } Response.Close(); } } //使用BinaryWrite分块下载文件 protected void btnDL5_Click(object sender, EventArgs e) { //指定区块和缓冲区 long chunkSize = 102400; byte[] buffer = new byte[chunkSize]; FileStream stream = null; long dataToRead = 0; try { stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read); dataToRead = stream.Length; //添加Http头 Response.ContentType = "application/octet-stream"; Response.AddHeader("Content-Disposition", "attachement;filename=" + fileName); Response.AddHeader("Content-Length", dataToRead.ToString()); while (dataToRead > 0) { if (Response.IsClientConnected) { int length = stream.Read(buffer, 0, Convert.ToInt32(chunkSize)); Response.BinaryWrite(buffer); Response.Flush(); Response.Clear(); dataToRead -= length; } else { dataToRead = -1; } } } catch(Exception ex) { Response.Write("Error:" + ex.Message); } finally { if (stream != null) { stream.Close(); } Response.Close(); } } }
以上除了第四种不推荐以外,其他的都可以,但是个人感觉分块下载的要好一点。没有仔细测试,所以可能有问题。
注意:对于中文文件名要编码才能正确显示。对于长中文文件名(UTF8编码后大于153字节的中文)即使编码了,还是有问题的,大家可以参考这个链接。
http://hi.baidu.com/river_5566/blog/item/d66804cef8afb031b700c863.html
呵呵,好久没用.NET了,看了有还点困难
我发过上次写的给你看看,希望有对你有点帮助
public void doFile(string filepath)
{
string path = filepath; //文件的路径//Server.MapPath(filepath);
//初始化 FileInfo 类的实例,它作为文件路径的包装
FileInfo fi = new FileInfo(path);
//判断文件是否存在
if (fi.Exists)
{
//将文件保存到本机上
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(fi.Name));
Response.AddHeader("Content-Length", fi.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.Filter.Close();
Response.WriteFile(fi.FullName);
Response.End();
}
}
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
Response.BinaryWrite(bytes );
以文件流的方式 试看看呢
如果是text格式的话,一般IE默认是直接把文件打开
你换一个地方下载试试,我有过这种情况,你多试试地方,我就只知道这些了