檔案下載時,只彈出下載框,不在頁面開啟的方法。
阿新 • • 發佈:2019-01-27
<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下除錯成功
<%@ 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下除錯成功