//生成一个文件并提供下载
private void Button1_Click(object sender, System.EventArgs e)
{
    //显示进度条
    string jsBlock="<script>div1.style.visibility='visible';</script>";
    Page.RegisterClientScriptBlock("jsBlock1",jsBlock);
    //生成一个文件
    ...
    //隐藏进度条
    jsBlock="<script>div1.style.visibility='hidden';</script>";
    Page.RegisterClientScriptBlock("jsBlock2",jsBlock);
    //提供下载
    if(File.Exists(Path.Combine(path,file)))
   {
FileInfo DownloadFile=new FileInfo(Path.Combine(path,file));
Response.Clear();
Response.ClearHeaders();
Response.Buffer=false;
Response.ContentType="application/octet-stream";
Response.AppendHeader("Content-Disposition","attachment;filename="+HttpUtility.UrlEncode(DateTime.Now.ToString("yyyyMMdd")+"("+Session["dwmc"].ToString()+").doc",System.Text.Encoding.UTF8));
Response.AppendHeader("Content-Length",DownloadFile.Length.ToString());
Response.WriteFile(DownloadFile.FullName);
Response.Flush();
Response.End();
    }运行后网页错误提示:服务器无法在发送HTTP标头之后清除标头。提示在
Response.ClearHeaders();

Response.ContentType="application/octet-stream";
出错。请问大虾怎么解决这个问题?

解决方案 »

  1.   

    你是不是要在mime里加上doc的类型?
      

  2.   

    这个不能写在Button_Click里面。因为当发生Button_Click的时候,页面的部分Header已经送到客户端了,而同时服务器端在处理Button_Click并准备发送剩下来的内容。你要发送文档,最好点击Button就打开一个新的aspx页面,该页面直接在Page_Load里面发送文件。
      

  3.   

    public static void DownloadFile(string physicalFilePath)
    {
    FileStream stream=null;
    stream = new FileStream(physicalFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);    
    int bufSize = (int)stream.Length;
    byte[] buf = new byte[bufSize]; int bytesRead = stream.Read(buf, 0, bufSize);
    HttpContext.Current.Response.ContentType = "application/octet-stream"; 
    HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename="+System.IO.Path.GetFileName(physicalFilePath));
    HttpContext.Current.Response.OutputStream.Write(buf, 0, bytesRead);
    HttpContext.Current.Response.End();
    }
      

  4.   

    在Button1_Click发送文档是可以的,也不需要另开一页来处理。问题是在显示和隐藏进度条的时候有Header发送给客户端,所以不能清除正在处理的Header和修改应用类型。
    要显示进度条的话,另开一页应该是个办法。可以试试。
      

  5.   

    同意  :cat_hsfz(我的Blog在http://purl.oclc.org/NET/cathsfz)
      

  6.   

    soft_ladybug(瓢虫) 说得对,另开一页来处理也很麻烦。难道就没有一个解决生成文件并提供下载,而且还有进度条显示的方案吗?
      

  7.   

    试了一下你的代码,在发送文档的条件下
    string jsBlock="<script>div1.style.visibility='visible';</script>";
        Page.RegisterClientScriptBlock("jsBlock1",jsBlock);
        //生成一个文件
        ...
        //隐藏进度条
        jsBlock="<script>div1.style.visibility='hidden';</script>";
        Page.RegisterClientScriptBlock("jsBlock2",jsBlock);这两个jsBlock根本没有被发送到客户端。
    如下是可行的,两个jsBlock可以被发送到客户端,能被执行:    string jsBlock="<script>div1.style.visibility='visible';</script>";
        Page.RegisterClientScriptBlock("jsBlock1",jsBlock);
        for(int i=0;i<100000;i++)
    for(int j=0;j<10000;j++){//do}
        //隐藏进度条
        jsBlock="<script>div1.style.visibility='hidden';</script>";
        Page.RegisterClientScriptBlock("jsBlock2",jsBlock);到此为止,下面不发送文档。我也觉得奇怪,为什么加了发送文档的代码后,jsBlock没有被发送?哪位高人能解释一下。