页面上一个输入信息的地方你输入一篇文章,点提交后,在文章内容保存到数据库后,同时把刚才输入的文字转成txt 然后弹出下载框  之后就能下载了。请问具体应该如何实现呢

解决方案 »

  1.   

    转成txt后总得保存到一个地方吧?
    把这个地方的路径重定向打开,就可以了
      

  2.   

    写入数据库同时生成txt文件。页面内加个link并设置隐藏,文件生成后link加上txt的存放路径,并触发事件把link的地址打开。
      

  3.   

    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" />&nbsp;</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;
            }
        }}
      

  4.   

    我改写了一下: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[]  = { 0xFF, 0xFE };
                outBuff[0] = [0];
                outBuff[1] = [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
            {        }    } }