请问这个功能如何实现呢

dnvvj 2008-05-17 05:36:02
页面上一个输入信息的地方你输入一篇文章,点提交后,在文章内容保存到数据库后,同时把刚才输入的文字转成txt 然后弹出下载框 之后就能下载了。请问具体应该如何实现呢
...全文
67 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
txgaozhao 2008-05-18
  • 打赏
  • 举报
回复
事物处理起来很复杂
skyaspnet 2008-05-18
  • 打赏
  • 举报
回复
我改写了一下:

test.aspx:


<%@ 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>



test.aspx.cs:


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
{

}

}

}

skyaspnet 2008-05-17
  • 打赏
  • 举报
回复
default.aspx:

<%@ 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>


default.aspx.cs:

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)
{

}
}


txt.ashx:

<%@ 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;
}
}

}
dnvvj 2008-05-17
  • 打赏
  • 举报
回复
有没有简单一些的代码呢。有点难哦
cylot 2008-05-17
  • 打赏
  • 举报
回复
写入数据库同时生成txt文件。页面内加个link并设置隐藏,文件生成后link加上txt的存放路径,并触发事件把link的地址打开。
job_2006 2008-05-17
  • 打赏
  • 举报
回复
转成txt后总得保存到一个地方吧?
把这个地方的路径重定向打开,就可以了
Haoze 2008-05-17
  • 打赏
  • 举报
回复
这问题好大……。

还是等别人回答吧,我现在有点儿懒。

62,046

社区成员

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

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

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

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