int row = ((GridViewRow)((LinkButton)sender).NamingContainer).RowIndex; //最简洁最高效的代码 LinkButton link1 = (LinkButton)Grid_Cart.Rows[row].Cells[1].FindControl("LinkButton2");
string fileName = link1.Text.Trim(); ;//客户端保存的文件名
string filePath = Server.MapPath("UpLoadCart\\" + link1.Text.Trim());//路径
System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
if (fileInfo.Exists)
{
const long ChunkSize = 102400;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力
byte[] buffer = new byte[ChunkSize];
Response.Clear();
System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);
long dataLengthToRead = iStream.Length;//获取下载的文件总大小
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName));
while (dataLengthToRead > 0 && Response.IsClientConnected)
{
int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小
Response.OutputStream.Write(buffer, 0, lengthRead);
Response.Flush();
dataLengthToRead = dataLengthToRead - lengthRead;
}
Response.Close();
}请大家看一下这些代码为什么不能实现下载功能,我设置断点时,他老是不运行 const long ChunkSize = 102400;//100K 这行代码
string fileName = link1.Text.Trim(); ;//客户端保存的文件名
string filePath = Server.MapPath("UpLoadCart\\" + link1.Text.Trim());//路径
System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
if (fileInfo.Exists)
{
const long ChunkSize = 102400;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力
byte[] buffer = new byte[ChunkSize];
Response.Clear();
System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);
long dataLengthToRead = iStream.Length;//获取下载的文件总大小
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName));
while (dataLengthToRead > 0 && Response.IsClientConnected)
{
int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小
Response.OutputStream.Write(buffer, 0, lengthRead);
Response.Flush();
dataLengthToRead = dataLengthToRead - lengthRead;
}
Response.Close();
}请大家看一下这些代码为什么不能实现下载功能,我设置断点时,他老是不运行 const long ChunkSize = 102400;//100K 这行代码
{
if (strPath != null && File.Exists(strPath))
{
System.IO.FileInfo file = new System.IO.FileInfo(strPath);
//清除缓冲区流中的所有内容输出 Response.Clear(); //将下载保存对话框指定默认的文件名添加到HTTP头中
//Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name); Response.AddHeader("Content-Disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8));//避免中文出现乱码现象 //在header中指定文件的大小,使浏览器能显示下载过程
Response.AddHeader("Content-Length", file.Length.ToString()); //设置输出流的 HTTP MIME 类型
Response.ContentType = "application/octet-stream"; // 发送文件流到客户端
Response.WriteFile(file.FullName);
// 停止该页的执行 Response.End(); }
else
{
Response.Write("文件自动下载中断,请手动下载");
}
}可以的话试下这个.