How to read a text file in asp classic

I upload an image. When uploading images, I save the image name and link for this iage in one text file. like this, abc.jpeg, http://google.com

Now I want to display all images with corresponding links using classic asp.

How can I do it?

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 display of all images, but the same for all image links. Reads directly the last link from a text file .... What is the problem with my code?

+4
source share
2 answers

You can 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 
+6
source

Your problem is how you assign a URLString. It starts as "", and when you read each line in the file, you overwrite the existing value. The last line of the file will be the last rewrite, so this will be the value inside the URLString at the end of the loop. Code example:

 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 the extra spaces around any text and simply records a list of serial images. The only drawback is that if the file name has a comma in it, it will break.

+1
source

All Articles