62,046
社区成员
发帖
与我相关
我的任务
分享
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>
<!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:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
<br />
请输入内容:
<br />
<br />
<asp:TextBox ID="TextBox1" runat="server" Height="192px" TextMode="MultiLine" Width="252px"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="生成下载文件" /></div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, System.EventArgs e)
{
try
{
string str = TextBox1.Text.ToString().Trim();
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 = TextBox2.Text.ToString().Trim();
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
{
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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:TextBox ID="TextBox1" runat="server" Height="173px" TextMode="MultiLine" Width="304px"></asp:TextBox><br />
<br />
<asp:Button ID="Button1" runat="server" Text="得到TXT文件" PostBackUrl="~/txt.ashx" /> </div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
<%@ WebHandler Language="C#" Class="txt" %>
using System;
using System.Web;
public class txt : IHttpHandler {
public void ProcessRequest(HttpContext context)
{
string FullFileName = @"c:\1.txt";
System.IO.FileInfo DownloadFile = new System.IO.FileInfo(FullFileName);
context.Response.Clear();
context.Response.ClearHeaders();
context.Response.Buffer = false;
context.Response.ContentType = "application/octet-stream";
context.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(DownloadFile.FullName, System.Text.Encoding.UTF8));
context.Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
context.Response.WriteFile(DownloadFile.FullName);
context.Response.Flush();
context.Response.End();
}
public bool IsReusable {
get {
return false;
}
}
}