ASP.net VB.net Image not appearing

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

Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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 BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor 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 ConsultantCommented:
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.
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

NorieAnalyst Assistant Commented:
Murray

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

     Image1.ImageUrl = FilePath

Open in new window

Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor 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 BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor 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 BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
I put the full url in and it worked    Image1.ImageUrl = "http://xxxxxi.azurewebsites.net/Uploads/" & oSelectedFile"
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
As mentioned, the full url worked

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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 BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
thanks Norie
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.