How to read text file in classic asp

后端 未结 3 1819
迷失自我
迷失自我 2020-12-11 07:18

I am uploading image.While uploading images i am saving image name and the link for that iage in one textfile. like this, abc.jpeg,http://google.com

Now i want to di

相关标签:
3条回答
  • 2020-12-11 07:47

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Text;
    // Fajlovi
    using System.IO;
    
    namespace A7
    {
        public partial class redvoznje : System.Web.UI.Page
        {
            class Red {
                public int Rbr { get; set; }
                public string Vreme { get; set; }
            }
            protected void Page_Load(object sender, EventArgs e)
            {
                
                if (!this.IsPostBack) {
    
                
                    string[] fajlovi = Directory.GetFiles(Server.MapPath("linije"));
    
                    for (int i = 0; i < fajlovi.Length; i++) { 
                      
                        string ime = Path.GetFileNameWithoutExtension(fajlovi[i]);
    
                      
                        LinijaLista.Items.Add(ime);
                    }
    
                    
                    string trenutnaLinija = LinijaLista.Text;
    
                    using (StreamReader sr =
                        new StreamReader(Server.MapPath("linije" + "/" + trenutnaLinija + ".txt"))) {
                            string linija;
                           
                            while ((linija = sr.ReadLine()) != null) {
                               
                                if (linija.StartsWith("SMER")) {
                                    string smer = linija.Substring(5);
                                    SmerLista.Items.Add(smer);
                                }
                            }
                    }
    
    
                }
    
    
    
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
             
                Table1.Rows.Clear();
    
               
                string trenutnaLinija = LinijaLista.Text;
                string smer = SmerLista.Text;
    
               
                List<Red> redovi = new List<Red>();
    
                using (StreamReader sr =
                        new StreamReader(Server.MapPath("linije" + "/" + trenutnaLinija + ".txt")))
                {
                    string linija;
                    bool stani = false;
                    bool pronadjenaLinija = false;
                    int rbr = -1;
    
                 
                    while ((linija = sr.ReadLine()) != null)
                    {
                        if (linija.StartsWith("SMER:" + smer))
                        {
                            pronadjenaLinija = true;
                        }
    
                        if (pronadjenaLinija)
                        {
                            if (linija.StartsWith("SMER") && !linija.StartsWith("SMER:" + smer))
                            {
                                stani = true;
                            }
    
                            if (!stani)
                            {
                                rbr++;
                                redovi.Add(new Red() { Rbr = rbr, Vreme = linija });
                            }
                        }
                    }
                }
                
                redovi.RemoveAt(0);
    
               
                TableRow zaglavlje = new TableRow();
    
                TableCell kol1 = new TableCell();
                TableCell kol2 = new TableCell();
    
                kol1.Text = "Redni broj polaska";
                kol2.Text = "Vreme polaska";
    
                zaglavlje.Controls.Add(kol1);
                zaglavlje.Controls.Add(kol2);
    
                Table1.Rows.Add(zaglavlje);
    
                for (int i = 0; i < redovi.Count; i++)
                {
                    TableRow red = new TableRow();
    
                    TableCell rbrKol = new TableCell();
                    TableCell vremeKol = new TableCell();
    
                    rbrKol.Text = redovi[i].Rbr.ToString();
                    vremeKol.Text = redovi[i].Vreme;
    
                    red.Controls.Add(rbrKol);
                    red.Controls.Add(vremeKol);
    
                    Table1.Rows.Add(red);
                }
    
    
                Table1.Visible = true;
    
            }
    
            protected void LinijaLista_SelectedIndexChanged(object sender, EventArgs e)
            {
                    SmerLista.Items.Clear();
    
                   
                    string trenutnaLinija = LinijaLista.Text;
    
                    using (StreamReader sr =
                        new StreamReader(Server.MapPath("linije" + "/" + trenutnaLinija + ".txt")))
                    {
                        string linija;
                     
                        while ((linija = sr.ReadLine()) != null)
                        {
                            
                            if (linija.StartsWith("SMER"))
                            {
    
                                string smer = linija.Substring(5);
    
                                SmerLista.Items.Add(smer);
                            }
                        }
                    }
                
    
    
            }
        }
    }

    0 讨论(0)
  • 2020-12-11 07:49

    You could try something like this -

    Dim lineData
    Set fso = Server.CreateObject("Scripting.FileSystemObject") 
    set fs = fso.OpenTextFile(Server.MapPath("imagedata.txt"), 1, true) 
    Do Until fs.AtEndOfStream 
        lineData = fs.ReadLine
        'do some parsing on lineData to get image data
        'output parsed data to screen
        Response.Write lineData
    Loop 
    
    fs.close: set fs = nothing 
    
    0 讨论(0)
  • 2020-12-11 08:00

    Your problem is the way you assign URLString. It starts off as "", and as you read through each line in the file, you overwrite it's existing value. The last line of the file will be the last overwrite, so that'll be the value inside of URLString at the end of the loop. An example of code is:

    output = ""
    path = server.mappath("../admin/Links.txt")
    set fs = server.createobject("Scripting.FileSystemObject")
    set f = fs.OpenTextFile(path, 1, true) '1 = for reading
    do while not f.AtEndOfStream
        text = trim(f.ReadLine)
        if text <> "" then
            if instr(text,",") > 0 then
                arry = split(text,",")
                ' assuming line = filename.jpg, url.com
                output = output & "<a href="""&trim(arry(1))&"""><img src="""&trim(arry(0))&""" /></a><br />"
            end if 
        end if
    loop
    f.close
    set f = nothing
    set fs = nothing
    

    This removes extra whitespace around any text and simply writes a list of series images. The one flaw this has is that if a filename has a comma in it, it'll break.

    0 讨论(0)
提交回复
热议问题