Greetings
I am absolute beginner in ASP.NET, and as such i tried to practice what i learned so far by making small image gallery.
This is code i have written so far, but after i run it, instead of getting pictures, i get small boxes with file names in them ?
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim files() As String
Dim i As Integer
files = Directory.GetFiles(Server.MapPath("~\Img"))
Dim smallImg(files.Length) As ImageButton
For i = 0 To files.Length - 1
smallImg(i) = New ImageButton()
smallImg(i).ImageUrl = files(i).ToString
smallImg(i).Width = 160
smallImg(i).Height = 100
smallImg(i).BorderStyle = BorderStyle.Inset
smallImg(i).BorderWidth = 2
smallImg(i).AlternateText = System.IO.Path.GetFileName(files(i).ToString())
smallImg(i).PostBackUrl = "default.aspx?Img=" & files(i).ToString()
Me.Panel1.Controls.Add(smallImg(i))
Next
End Sub
End Class
can someone please explain me what i did wrong and point, or suggest better solution.
P.S. i know that there are a lot of free pre-made solutions for this, but i would really like to learn this without using components.
Thanks in advance
What happens when you debug files(i).ToString()? Does it give you the local path to the image? Or, does it just give you the file name? You will likely have to convert this to a web URL.
NetProfit:
What happens when you debug files(i).ToString()? Does it give you the local path to the image? Or, does it just give you the file name? You will likely have to convert this to a web URL.
When debugging for files(i).ToString i get full local path, for instance "C:\Documents and Settings\Shedvel\Desktop\Dev\WebSite1\Img\000007.jpg". so it should display image right ?
Also if it's not to much to ask, ho can i convert those to web URL, as you mentioned?
That might display on your local machine depending on how the control renders the src attribute in the HTML that is sent to your browser. Of course, it would not work for anyone else.
View the HTML source from your web browser and show what the result is there.
To convert this to a web URL, you need just the filename, not the entire path. Then, you sould be able to build the image URL knowing what folder the images are stored in relative to your web application directory.
See if you can figure this out and let me know if you need any more help.
Thanks,
I have tried to simplify code, so i just get one picture
Dim files() As String
Dim i As Integer
files = Directory.GetFiles(MapPath("~/img/"))
slika.ImageUrl = files(1)
and it does not return any errors, but does not display image either. in source view i get :
...[ <img id="slika"src="C:\Documents%20and%20Settings\Shedvel\Desktop\Dev\WebSite1\img\007t.jpg"style="height:695px;width:792px;border-width:0px;"/> ]...
for img, which is exact address of image file. So why it does not display it then.
i am starting to fell stupid...
The problem is probably the spaces contained in the path. Locate your site in another folder on your C drive, e.g. C:\Dev\WebSite1 (no spaces).
You can test this by putting your image in another path without spaces and hard-coding the path in your code-behind (may need to give .NET read rights to the folder).
I think there is likely a problem with the URL encoded space characters "%20". That being said, this is not the behavior you are looking for because this will work only on your machine. You should use the DirectoryInfo and FileInfo classes in this case, get the filename, and append it to a relative path to your img folder:
Dim pathAs String = Server.MapPath("img")Dim diAs New IO.DirectoryInfo(path)Dim fiAs IO.FileInfoFor Each fiIn di.GetFiles()Dim iAs New ImageMe.Page.Controls.Add(i) i.ImageUrl ="img/" + fi.NameNextThank you very, very much.
That code solved problem, plus i finally understand URL encoding.
0 comments:
Post a Comment