ASP.net VB.net Image not appearing

Murray Brown
Murray Brown used Ask the Experts™
on
Hi

In my ASP.net VB.net web app I am trying to view an image in one of my folders using the following two lines but the image is not appearing

     Dim FilePath As String = Server.MapPath("~/Uploads/" & oSelectedFile)
     Image1.ImageUrl = FilePath

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
NorieAnalyst Assistant

Commented:
Murray

Is the value FilePath definitely a valid URL for the image you want to display?

Where is the image actually located/stored?
Murray BrownASP.net/VSTO Developer

Author

Commented:
I was pretty sure it was until you asked. Should I use code to validate that it is? System.IO?
Rikin ShahMicrosoft Dynamics CRM Consultant

Commented:
Hi,

First check what Server.MapPath() returns. It will return only the file name and not whole path. You need to build that url with concatenating strings of primary url and file name.
Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

NorieAnalyst Assistant

Commented:
Murray

Try this.
     Dim FilePath As String = Server.MapPath("~/Uploads/") & oSelectedFile

     Image1.ImageUrl = FilePath

Open in new window

Murray BrownASP.net/VSTO Developer

Author

Commented:
Hi Norie, Thanks but I already had that. Rikin I put a watch on my Filepath variable and it was C:\Users\murbr\Documents\Visual Studio 2017\Projects\LesediWebApp\LesediWebApp\Uploads\Image2.jpg
If I then paste that to the  ImageUrl property of my image. I see the image. I have a suspicion that it might need the full Url of the web location
NorieAnalyst Assistant

Commented:
Murray

When you tried what I suggested what was the value of FilePath?
Murray BrownASP.net/VSTO Developer

Author

Commented:
Hi Norie. My original code was as follows.Is that not exactly the same as yours which I pasted next?

  Dim FilePath As String = Server.MapPath("~/Uploads/" & oSelectedFile)
     Image1.ImageUrl = FilePath

Open in new window


Your was the following
 Dim FilePath As String = Server.MapPath("~/Uploads/") & oSelectedFile
     Image1.ImageUrl = FilePath

Open in new window

NorieAnalyst Assistant
Commented:
Murray

No, the 2 sets of code are different.

As far as I'm aware you use Server.MapPath to get a path to a folder.

So this,
Server.MapPath("~/Uploads/")

Open in new window

should get you the path to the Uploads folder.

Assuming you are storing the image(s) you want to show in the Uploads folder you would then concatenate the file name to get the full URL.

Perhaps it's clearer if written like this.
 Dim FilePath As String = Server.MapPath("~/Uploads/")
    
     Image1.ImageUrl = FilePath & oSelectedFile

Open in new window


By using Server.MapPath("~/Uploads/" & oSelectedFile) the code is probably trying to locate a subfolder within the Uploads folder with the name of whatever is in oSelectedFile.
Murray BrownASP.net/VSTO Developer

Author

Commented:
I put the full url in and it worked    Image1.ImageUrl = "http://xxxxxi.azurewebsites.net/Uploads/" & oSelectedFile"
ASP.net/VSTO Developer
Commented:
As mentioned, the full url worked
NorieAnalyst Assistant

Commented:
You need to check if what you have is returning the full URL.

What does this on it's own return?
Dim FilePath As String = Server.MapPath("~/Uploads/")

Open in new window

Does it return 'http://xxxxxi.azurewebsites.net/Uploads/'?

What if you drop the '~' at the start and the '/' end?
Dim FilePath As String = Server.MapPath("/Uploads"

Open in new window

Murray BrownASP.net/VSTO Developer

Author

Commented:
thanks Norie

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial