gridview 导出excel 迅雷下载时问题 急求解决方法!

wangkuixing 2010-07-28 12:39:14
在gridview导出excle时,若遇到迅雷下载就会出问题,下载的是.aspx页面,有时候还下载不了。

代码如下:
protected void Button_Save_Click(object sender, EventArgs e) //保存到Excel表中
{
GridView2.AllowPaging = false; //分页隐藏
GridView2.Columns[12].Visible = false; //删除列隐藏
GridView2.BottomPagerRow.Visible = false; //下面的分页设置隐藏
GridView2.DataBind();

StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter htw = new HtmlTextWriter(sw);

Page page = new Page();
HtmlForm form = new HtmlForm();

GridView2.EnableViewState = false;
page.EnableEventValidation = false;
page.DesignerInitialize();

page.Controls.Add(form);
form.Controls.Add(GridView2);

page.RenderControl(htw);
Response.Clear(); //清空输出流
Response.Buffer = true; //开启缓冲输出
Response.ContentType = "application/vnd.ms-excel";
string filename = DateTime.Now.ToString("d");//获取时间(作为文件名)
Response.AddHeader("Content-Disposition", "attachment;filename=" + filename + ".xls");//设置存成的文件名字
Response.Charset = "GB2312"; //解决中文乱码的问题
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
Response.ContentEncoding = System.Text.Encoding.UTF7;

Response.Write(sb.ToString());
Response.End();

GridView2.Columns[12].Visible = true;
GridView2.BottomPagerRow.Visible = true;
GridView2.AllowPaging = true;
GridView2.DataBind();

}
...全文
721 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangkuixing 2011-11-14
  • 打赏
  • 举报
回复
好久了,都忘了结贴了
冰封剑心 2010-08-14
  • 打赏
  • 举报
回复
  if (File.Exists(filepath))
{
string filename = strFileName;

System.IO.FileInfo file = new FileInfo(filepath);


FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read);//file.Create();
BinaryReader br = new BinaryReader(fs);
if ((int)fs.Length==0)
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "str", "<script>alert(\"文件不存在!!\")</script>");
return;
}
byte[] byteTemp = br.ReadBytes((int)fs.Length);
fs.Close();
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.Clear();
Encoding code = Encoding.GetEncoding("gb2312");
response.ContentEncoding = code;
response.HeaderEncoding = code;
string SufName=string.Empty;
SufName=GetSufName(filename);
if (SufName == "tif")
{
response.AddHeader("Content-Type", "image/tiff");
}
else if (SufName == "txt")
{
response.AddHeader("Content-Type", "text/plain");

}
else if (SufName == "doc")
{
response.AddHeader("Content-Type", "application/msword");

}
else if (SufName == "rar")
{
response.AddHeader("Content-Type", "application/octet-stream");

}
else
{
response.AddHeader("Content-Type", "application/octet-stream");

}

if (Request.QueryString["LoadType"].ToString() == "download" )
{

response.AppendHeader("Content-Disposition",
"attachment; filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8) + "; size=" + byteTemp.Length.ToString());
}
else {
response.AppendHeader("Content-Disposition",
"inline; filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8) + "; size=" + byteTemp.Length.ToString());
}
response.Flush();
response.BinaryWrite(byteTemp);
response.Flush();
response.End();
}
else
{
Response.Write("<script>alert('附件所对应的物理文件已经在应用程序外被删除!')</script>");
Response.Write("<script>window.history.back()</script>");
}


类似问题:我的只有在tif文件格式的时候出现 迅雷弹出. 目前使用的方法“不监视网站"。可以考虑直接在程序里面修改迅雷设置,如ie加载项里面去掉或者把烦人的迅雷屏蔽。

Adechen 2010-07-28
  • 打赏
  • 举报
回复
设置两次contextType是为什么呀
wwfgu00ing 2010-07-28
  • 打赏
  • 举报
回复

用文件流
/// <summary>
/// 指定要下载文件的虚拟路径及文件名
/// </summary>
/// <param name="FileName"></param>
public void downloadfile(string FileName)
{

//打开要下载的文件
System.IO.FileStream r = new System.IO.FileStream(Server.MapPath(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.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();//结束文件下载
}
YnSky 2010-07-28
  • 打赏
  • 举报
回复
在迅雷里有设置.不监视网页就好了,点击下载就不启动迅雷了
wangkuixing 2010-07-28
  • 打赏
  • 举报
回复
contexType是我多写了一次,完全可以去掉一句。
暖枫无敌 2010-07-28
  • 打赏
  • 举报
回复
这个好像跟IE浏览器的设置有点关系,我以前也遇到过,除了改变下设置外,我还真没找到解决办法,期待高手。
Devillyd 2010-07-28
  • 打赏
  • 举报
回复
遇到同样的问题,关注
myhope88 2010-07-28
  • 打赏
  • 举报
回复
一般来说,contenttype有设定,而且headers中设定attachement了,应该不会有问题吧
代码下载地址: https://pan.quark.cn/s/ee8627e4e6d7 ABAP调试器是一种功能强大的工具,可用于在执行期间对ABAP代码进行检验。除了常规的核心功能(例如逐行运行代码以及检验变量、字段符号和引用的值)之外,它还提供了一些辅助性的特性,能够简化并压缩调试会话的长。并非所有使用者都熟悉这些辅助特性。SAP ABAP调试器是处理和优化ABAP代码开发与维护工作的核心资源,它配备了多样的功能来协助开发人员在运行状态下进行检验和排除故障。此资源着重阐述了ABAP调试器的一些高级特性,涵盖了深入分析调用堆栈、系统级调试、更新会话调试以及提升调试效率的方法。 1. **深入分析调用堆栈**:除了常规的应用程序调试,开发人员有需要对调用堆栈的内部层级进行深入调试,特别是在错误出现在异步执行的更新处理或系统级程序。通过启用**系统级调试**,可以访问通常不公开的系统代码,但这也会导致调用堆栈的显著增加,因此需要审慎操作。 2. **系统级调试**:对于不含业务逻辑的系统级程序,开发人员通常无需进行调试。然而,在特定情形下,例如进行错误追踪,可能需要进入系统代码。借助调试器的“系统调试启用/禁用”选项,可以赋予对系统程序的调试权限。 3. **更新会话调试**:在处理异步更新任务,例如持久化业务数据,错误可能发生在更新任务内部。激活**更新会话调试**,在更新任务完成后,调试器将自动启动,展示执行路径。比如,在变更成本中心后,通过输入调试指令 "/h" 启动调试,保存后能够看到更新过程中的错误。 4. **分析调用堆栈**:在进行深入调试,调用堆栈是至关重要的。通过分析调用堆栈,能够定位到引发问题的具体位置,如在VB_V2_NORMAL...

62,271

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧