string fileName;
LinkButton tb = (LinkButton)sender;//按钮
GridViewRow gv = (GridViewRow)tb.Parent.Parent;//数据视图对象
fileName = ((Label)GridView1.Rows[gv.RowIndex].Cells[0].FindControl("Label1")).Text;
string filePath = Server.MapPath(".") + "\\Attachment\\" + fileName;//路径
FileInfo fileInfo = new FileInfo(filePath);
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
Response.AddHeader("Content-Length", fileInfo.Length.ToString());
Response.AddHeader("Content-Transfer-Encoding", "binary");
Response.ContentType = "application/octet-stream";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Response.WriteFile(fileInfo.FullName);
Response.Flush();
Response.End();
上面的代码用来下载文件,为什么有时候下载的对话框会一闪而过,有时候会下载代码所在的页面,有时候会提示无法下载,有时候能正常下载
LinkButton tb = (LinkButton)sender;//按钮
GridViewRow gv = (GridViewRow)tb.Parent.Parent;//数据视图对象
fileName = ((Label)GridView1.Rows[gv.RowIndex].Cells[0].FindControl("Label1")).Text;
string filePath = Server.MapPath(".") + "\\Attachment\\" + fileName;//路径
FileInfo fileInfo = new FileInfo(filePath);
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
Response.AddHeader("Content-Length", fileInfo.Length.ToString());
Response.AddHeader("Content-Transfer-Encoding", "binary");
Response.ContentType = "application/octet-stream";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Response.WriteFile(fileInfo.FullName);
Response.Flush();
Response.End();
上面的代码用来下载文件,为什么有时候下载的对话框会一闪而过,有时候会下载代码所在的页面,有时候会提示无法下载,有时候能正常下载
解决方案 »
- GridView1_SelectedIndexChanged事件获取当前行的值代码如何写
- 如果要在网站上获取客户的ip并保存在数据库中在asp.net中怎么实现的?
- 请教怎么记录分享人,在网站上面加了分享微博。怎么记录是谁分享的。
- .net 实现计划任务
- 大家帮我顶一下啊?》》》》》》》》》》》》》》
- 谁有asp.net2.0的分页控件,分页控件有下拉框可以选项页码的那种,谢谢了
- 文件下载的问题,对你来说应该不是很难
- 大家帮助看看这个报错什么意思
- 为什么用FileStream 可以写一个文件,但用dataset.WriteXml就提示访问被拒绝呢?
- 高手进,帮我解决下存储过程中使用like遇到的问题
- 团购秒杀用什么连接模式写好一些
- jason数据如何转成XML数据?
输出内容有中文吧,换成utf-8编码试试。