使用ASP写入文本文件

时间:2014-10-21 10:20:12

标签: asp-classic

我是ASP的新手,所以任何帮助都会非常感激。我有一个html页面,其中有一个表单供用户留下他们的详细信息,当他们提交它的文本文件很好,但我想要的是,一旦他们提交了它我想要一个警告说“评论已保存”并留下在原始页面上,如果他们选择,他们可以提交另一个,但是当用户提交时,它会转到空白页面。 我的表格是

<form method="post" action="comments.asp">
<br><br>
Age <input type="text" name="age" />
Name<textarea rows="1" cols="70" <input type="text" name="name" /></textarea> 
<input type="submit" value="Send Comment">
</form>

我的comments.asp文件代码是

<%
Dim age, name
age = Request.Form("age")
name = Request.Form("name")
dim fs,f
set fs=Server.CreateObject("Scripting.FileSystemObject")
set f=fs.OpenTextFile("C:\Inetpub\wwwroot\Wiki\Comments.txt",8,true)
f.WriteLine(age & " " & date() & " " & name)
f.Close
set f=nothing
set fs=nothing
%>

3 个答案:

答案 0 :(得分:4)

<%
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
'Open the file for writing
Set f = fso.CreateTextFile(s_path & "/" & s_file_being_created, True)
f.Write(m)
f.Close
Set f = Nothing
Set fso = Nothing
%>

答案 1 :(得分:1)

对于那些使用JScript而不是VBS编写ASP的人,代码可能看起来像这样:

var fileSystemObject = Server.CreateObject("Scripting.FileSystemObject");
var textStream = fileSystemObject.CreateTextFile(filePath, true, true);
textStream.Write(fileContent);
textStream.Close();
delete textStream;
delete fileSystemObject;

文档参考:

答案 2 :(得分:0)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
// Za tekstualne fajlove
using System.IO;

namespace A6
{
    public partial class imanik : System.Web.UI.Page
    {
        public class RedTabele
        {
            public string sifra { get; set; }
   
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                
                List<string> mesta = new List<string>();

                using (StreamReader sr = new StreamReader(Server.MapPath("imenik.txt")))
                {
                    string line;
                    while ((line = sr.ReadLine()) != null)
                    {
                       
                        string[] podeljeno = line.Split('|');

                        string mesto = podeljeno[4];

                        mesto = mesto.Trim();

                        mesta.Add(mesto);



                    }
                }

                List<string> bezDuplikata = mesta.Distinct().ToList();
                for (int i = 0; i < bezDuplikata.Count; i++)
                {
                    ListaMesto.Items.Add(bezDuplikata[i]);
                }
            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
          
            Table1.Rows.Clear();
            Table1.Visible = false;

            
            string ime = txtIme.Text;
            string prezime = txtPrezime.Text;
            string adresa = txtAdresa.Text;
            string mesto = ListaMesto.Text;
            string br = txtTel.Text;

            
            List<RedTabele> rezultati = new List<RedTabele>();

            using (StreamReader sr = new StreamReader(Server.MapPath("imenik.txt")))
            {
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                   
                    string[] podeljeno = line.Split('|');

                    string sifraTXT = podeljeno[0];
                 

                    sifraTXT = sifraTXT.Trim();
                


                    bool ImeOk = true;
                    bool PrezimeOk = true;
                    bool AdresaOk = true;
                    bool MestoOk = true;
                    bool BrOk = true;

                    if (ime != "")
                    {
                        ImeOk = imeTXT.StartsWith(ime);
                    }

                



                    if (ImeOk && PrezimeOk && AdresaOk && MestoOk && BrOk)
                    {
                        rezultati.Add(new RedTabele
                        {
                            sifra = sifraTXT,
                           
                        });
                    }


                }

                // Create Table
                TableRow naslovna = new TableRow();
                Table1.Rows.Add(naslovna);

                TableCell tdSifra = new TableCell();
                tdSifra.Text = "Sifra";

           

                naslovna.Cells.Add(tdSifra);
              





                for (int i = 0; i < rezultati.Count; i++)
                {
                    TableRow red = new TableRow();

                    TableCell sifraKol = new TableCell();
                   

                    sifraKol.Text = rezultati[i].sifra;
                  

                    red.Controls.Add(sifraKol);
            

                    Table1.Rows.Add(red);
                }


                Table1.Visible = true;



            }
        }
    }
}