user985225
user985225

Reputation: 95

How to read text file in classic asp

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 display all that images with corresponding links using classic asp.

How should I do this?

Please help.

I used this asp code:

<%
For Each FileName in fold.Files
Dim Fname
Dim strFileName
Dim objFSO
Dim objTextFile
Dim URLString
Dim strReadLineText

Fname= mid(FileName.Path,instrrev(FileName.Path,"\\")+1)
strFileName = "../admin/Links.txt"
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

Set objTextFile = objFSO.OpenTextFile(Server.MapPath(strFileName))

URLString=""

Do While Not objTextFile.AtEndOfStream
    strReadLineText = objTextFile.ReadLine
    'response.Write(strReadLineText & "<br>")

    If strReadLineText<>"" then
        If Instr(strReadLineText,",")>0 then
            strReadLineTextArr=split(strReadLineText,",")
            response.Write(strReadLineTextArr(0))
            URLString=strReadLineTextArr(1)
        end if 
    end if
Loop

' Close and release file references

objTextFile.Close

Set objTextFile = Nothing

Set objFSO = Nothing

its displaying all images but for all images link is same.its reading directly the last link from textfile....What is the problem with my code?

Upvotes: 7

Views: 37194

Answers (3)

Marko
Marko

Reputation: 16

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);
                        }
                    }
                }
            


        }
    }
}

Upvotes: -1

Bradley Staples
Bradley Staples

Reputation: 367

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.

Upvotes: 2

ipr101
ipr101

Reputation: 24236

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 

Upvotes: 8

Related Questions