2011-10-08 78 views
4

我上傳image.While上傳圖像我保存圖像名稱和該iage鏈接在一個文本文件。 這樣, abc.jpeg,http://google.com如何閱讀經典的ASP文本文件

現在我想顯示所有的圖像與相應的鏈接使用經典的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 

其顯示所有圖像,但所有圖片鏈接same.its直接讀取文本文件,從最後一個環節....什麼是我的代碼的問題?

回答

6

你可以嘗試這樣的事情 -

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

謝謝,它適用於我。 – chenio

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 

這消除了周圍的任何文字多餘的空格,只是寫一系列圖像的列表。其中一個缺陷是,如果文件名中有一個逗號,就會中斷。