文件下载时,只弹出下载框,不在页面打开的方法。

<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
default4.aspx:  

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></div>
    </form>
</body>
</html>

default4.aspx.cs:

 

    protected void Button1_Click(object sender, System.EventArgs e) 
    {
        try 
        { 

        string str = "<html>"; 
        for(int page=0; page<4; page++) 
        { 
        str += "<TABLE borderColor='black' cellSpacing='0' borderColorDark='white' cellPadding='3' border='1'>"; 
        str += "<tr><th>描述</th></tr>"; 
        for(int i=1; i< 10; i++) 
        { 
        str =str + "<tr><td>测试" + i.ToString() + "</td></tr>"; 

        } 
        str +="</table>"; 

        //.doc 换页 
        str +="<br clear=all style='mso-special-character:line-break;page-break-before:always'>"; 
        } 

        str += "</html>"; 

        byte[] buff = System.Text.Encoding.Unicode.GetBytes(str); 
        //byte[] buff = System.Text.Encoding.UTF8.GetBytes(str); 

        byte[] outBuff = new byte[buff.Length + 2]; 

        // 使用文件流方式写入UniCode编码的doc文件。 
        byte[] mark = {0xFF,0xFE} ; 
        outBuff[0] = mark[0]; 
        outBuff[1] = mark[1]; 

        for(int i=0; i< buff.Length; i++) 
        { 
        outBuff[i+2] = buff[i]; 
        } 

        Context.Response.ContentType="application/octet-stream"; 
        string fileName = "测试.doc"; 
        Context.Response.AddHeader("Content-Disposition","attachment; filename=/"" + HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8) + "/""); 

        Context.Response.AddHeader("Content-Length",outBuff.Length.ToString()); 
        Response.BufferOutput = true; 
        Response.Clear(); 
        Context.Response.BinaryWrite(outBuff); 
        Context.Response.End(); 




        } 
        catch(Exception ex) 
        { 
        ex.ToString(); 
        } 
        finally 
        { 

        }

    }
在VS2005下调试成功
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值