如何在经典的asp中读取文本文件

时间:2011-10-08 09:45:26

标签: asp-classic

我正在上传图片。上传图片时,我将图片名称和该文件的链接保存在一个文本文件中。 像这样, abc.jpeg,HTTP://google.com

现在我想使用经典的asp。

显示所有带有相应链接的图像

我该怎么做?

请帮忙。

我使用了这个asp代码:

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

它显示所有图像但是对于所有图像链接是相同的。直接从文本文件中读取最后一个链接....我的代码有什么问题?

3 个答案:

答案 0 :(得分:7)

你可以尝试这样的事情 -

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 

答案 1 :(得分:1)

您的问题是您分配URLString的方式。它以“”开头,当您读取文件中的每一行时,您将覆盖它的现有值。该文件的最后一行将是最后一次覆盖,因此这将是循环结束时URLString内的值。代码的一个例子是:

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

这会删除任何文本周围的额外空格,只需编写一系列系列图像。这样做的一个缺陷是,如果文件名中包含逗号,它就会中断。

答案 2 :(得分:0)

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


        }
    }
}