62,046
社区成员
发帖
与我相关
我的任务
分享
private void DownloadFile(string filename)
{
//文件是否存在
if (!System.IO.File.Exists(filename))
{
Common.ShowMessage.Show(Page, "suess1", "文件不存在!");
return;
}
else
{
//打开要下载的文件
System.IO.FileStream r = new System.IO.FileStream(filename, System.IO.FileMode.Open);
//设置基本信息
Response.Buffer = false;
Response.AddHeader("Connection", "Keep-Alive");
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;filename=" + System.IO.Path.GetFileName(filename));
Response.AppendHeader("Content-Disposition", "attachment; filename=\"" + Server.UrlEncode(filename) + "\"");
Response.AddHeader("Content-Length", r.Length.ToString());
while (true)
{
//开辟缓冲区空间
byte[] buffer = new byte[1024];
//读取文件的数据
int leng = r.Read(buffer, 0, 1024);
if (leng == 0)//到文件尾,结束
break;
if (leng == 1024)//读出的文件数据长度等于缓冲区长度,直接将缓冲区数据写入
Response.BinaryWrite(buffer);
else
{
//读出文件数据比缓冲区小,重新定义缓冲区大小,只用于读取文件的最后一个数据块
byte[] b = new byte[leng];
for (int i = 0; i < leng; i++)
b[i] = buffer[i];
Response.BinaryWrite(b);
}
}
r.Close();//关闭下载文件
Response.End();//结束文件下载
}
}
在输出文件地地方使用的代码:
string path = request.PhysicalPath;//获取客户端请求的文件的物理地址
if (File.Exists(path))
{
string fileName = System.IO.Path.GetFileName(path); // 获取文件的名字
if (Request.UserAgent.Contains("MSIE") || Request.UserAgent.Contains("msie"))
{
// 如果客户端使用 Microsoft Internet Explorer,则需要编码
fileName = ToHexString(fileName); // 如果使用 fileName =Server.UrlEncode(fileName); 则会出现上文中出现的情况
}
Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
Response.WriteFile(path);
Response.End();
}
应该置于上述代码同一文件或可访问的其他类的几个函数:
/// <summary>
/// 为字符串中的非英文字符编码
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static string ToHexString(string s)
{
char[] chars = s.ToCharArray();
StringBuilder builder = new StringBuilder();
for (int index = 0; index < chars.Length; index++)
{
bool needToEncode = NeedToEncode(chars[index]);
if (needToEncode)
{
string encodedString = ToHexString(chars[index]);
builder.Append(encodedString);
}
else
{
builder.Append(chars[index]);
}
}
return builder.ToString();
}
/// <summary>
///指定 一个字符是否应该被编码
/// </summary>
/// <param name="chr"></param>
/// <returns></returns>
private static bool NeedToEncode(char chr)
{
string reservedChars = "$-_.+!*'(),@=&";
if (chr > 127)
return true;
if (char.IsLetterOrDigit(chr) || reservedChars.IndexOf(chr) >= 0)
return false;
return true;
}
/// <summary>
/// 为非英文字符串编码
/// </summary>
/// <param name="chr"></param>
/// <returns></returns>
private static string ToHexString(char chr)
{
UTF8Encoding utf8 = new UTF8Encoding();
byte[] encodedBytes = utf8.GetBytes(chr.ToString());
StringBuilder builder = new StringBuilder();
for (int index = 0; index < encodedBytes.Length; index++)
{
builder.AppendFormat("%{0}", Convert.ToString(encodedBytes[index], 16));
}
return builder.ToString();
}
FileInfo downloadFile = new FileInfo(Server.MapPath("~/C语言概念题解答选编.doc"));
long fileSize = downloadFile.Length;
Response.Clear();
Response.ContentType = "application/x-zip-compressed";
Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(downloadFile.Name));
//不指明Content-Length用Flush的话不会显示下载进度
Response.AddHeader("Content-Length", fileSize.ToString());
Response.TransmitFile(Server.MapPath("~/C语言概念题解答选编.doc"), 0, fileSize);
Response.Flush();
Response.Close();