?
Solved

File Exist on ASPX page using VB.net is not working

Posted on 2013-05-20
11
Medium Priority
?
512 Views
Last Modified: 2013-05-21
I have this Sub FileExist() that checks to see if a picture is in that location and either returns the pictures or returns a picture that says "no image available"

For some reason i can not get it to work. it keeps returning the 'nofile' response even though i know the 'file' is there.

Sub FileExists()
        Dim file, nofile As String
        file = "http://www.bakerabilene.com/BAMEQ/" & txtSearchBAM.Text & "/1.jpg"
        nofile = "http://www.bakerabilene.com/BAMEQ/NoImage.jpg"

        If System.IO.File.Exists(file) = True Then
            Image0.ImageUrl = file
        Else
            Image0.ImageUrl = nofile
        End If

    End Sub

Open in new window


I was trying to use the server.mappath which i have got to work on another project but i cant seem to get it to work either.
0
Comment
Question by:bignadad
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 14

Expert Comment

by:quizwedge
ID: 39182629
System.IO.File.Exists is expecting something in the format of "C:\BAMEQ\image1.jpg"

Check out http://msdn.microsoft.com/en-us/library/ms524632(v=vs.90).aspx for Server.MapPath.

I think System.IO.File.Exists(Server.MapPath("/BAMEQ/" & txtSearchBAM.Text & "/1.jpg")) will work or at least get you close, but I can't test it right now.
0
 
LVL 2

Author Comment

by:bignadad
ID: 39183007
imageI tried this and even typing a number that i know is there it still comes back to the 'Else' (no image)

Sub FileExists()
        Dim exist, file, nofile As String
        file = Server.MapPath("/BAMEQ/" & txtSearchBAM.Text & "/1.jpg")
        exist = System.IO.File.Exists(Server.MapPath("/BAMEQ/" & txtSearchBAM.Text & "/1.jpg"))
        MessageBox(file)
        nofile = "http://www.bakerabilene.com/BAMEQ/NoImage.jpg"

        If System.IO.File.Exists(exist) = True Then
            Image0.ImageUrl = file
        Else
            Image0.ImageUrl = nofile
        End If

    End Sub

Open in new window


the messagebox(file) shows me this

image
0
 
LVL 14

Expert Comment

by:quizwedge
ID: 39183088
Was able to do some testing. Try the following:
Sub FileExists()
        Dim exist, file, nofile As String
        file = Server.MapPath("/BAMEQ/") & txtSearchBAM.Text & "/1.jpg"
        exist = System.IO.File.Exists(Server.MapPath("/BAMEQ/") & txtSearchBAM.Text & "/1.jpg")
        MessageBox(file)
        nofile = "http://www.bakerabilene.com/BAMEQ/NoImage.jpg"

        If System.IO.File.Exists(exist) = True Then
            Image0.ImageUrl = file
        Else
            Image0.ImageUrl = nofile
        End If

    End Sub

Open in new window


Just the path should go in the Server.MapPath not the file.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 2

Author Comment

by:bignadad
ID: 39183098
its still not setting image.

messagebox(file) is the following
image
0
 
LVL 14

Expert Comment

by:quizwedge
ID: 39183120
All of the \'s are getting taken out and I'm not sure why. What happens if you do
messagebox(Server.MapPath(""))
0
 
LVL 2

Author Comment

by:bignadad
ID: 39183217
its weird because i have this working on a different page.

here is that code

Dim savedCSVFile As String
        Dim currentUserName As String
        Dim CSVFileandLocation As String
        currentUserName = (GetCurrent.Name)
        savedCSVFile = Path.GetFileName(currentUserName & ".csv")
        Me.FileUpload1.PostedFile.SaveAs(Server.MapPath("temp\") & savedCSVFile)
        CSVFileandLocation = Server.MapPath("temp\" & savedCSVFile)

Open in new window


here are these results

image
0
 
LVL 14

Expert Comment

by:quizwedge
ID: 39183442
I'm confused about why all of the \'s are taken out. It's probably the messagebox control you're using. Instead of message box can you try

Response.Write("look here:" & Server.MapPath(""))

You should see look here on the page and then the path with the slashes. Once we get the slashes, solving the rest of the issue should be easy.
0
 
LVL 2

Author Comment

by:bignadad
ID: 39184277
K. it must be my messagebox removing the \'s

here is the result of respone.write
look here:D:\Hosting\11100286\html

Also, here is my messagebox function. any quick way to fix it?
Private Sub MessageBox(ByVal strMsg As String)

        'Javascript message box that allows popups. Cannot set titles. Jquery is something to look into for more customizations
        Dim lbl As New Label
        lbl.Text = "<script language='javascript'>" & Environment.NewLine _
                   & "window.alert(" & "'" & strMsg & "'" & ")</script>"
        Page.Controls.Add(lbl)
    End Sub

Open in new window

0
 
LVL 2

Author Comment

by:bignadad
ID: 39184336
okay so i tried this

Response.Write(file)

and here is the result

D:\Hosting\11100286\html\BAMEQ\1013\1.jpg
0
 
LVL 2

Author Comment

by:bignadad
ID: 39184595
I got it working like this. don't know why server.mappath wouldnt work

Sub FileExists()
        Dim exist, file, nofile As String
        file = "http://www.bakerabilene.com/BAMEQ/" & txtSearchBAM.Text & "/1.jpg"
        exist = System.IO.File.Exists(Server.MapPath("\BAMEQ\") & txtSearchBAM.Text & "\1.jpg")
        nofile = "http://www.bakerabilene.com/BAMEQ/NoImage.jpg"

        If exist = True Then
            Image0.ImageUrl = file
        Else
            Image0.ImageUrl = nofile
        End If

    End Sub

Open in new window

0
 
LVL 14

Accepted Solution

by:
quizwedge earned 2000 total points
ID: 39184925
Sorry, sent that function to you quickly and realized it had some bugs. Your function is correct. I messed up by using Server.MapPath when saving file and also using System.IO.File.Exists a second time in my line 8. Your function is fine, but if you want to clean it up a little bit, you could try the following

Sub FileExists()
        Dim exist, file, nofile As String
        file = "http://www.bakerabilene.com/BAMEQ/" & txtSearchBAM.Text & "/1.jpg"
        nofile = "http://www.bakerabilene.com/BAMEQ/NoImage.jpg"

        If System.IO.File.Exists(Server.MapPath("\BAMEQ\") & txtSearchBAM.Text & "\1.jpg") Then
            Image0.ImageUrl = file
        Else
            Image0.ImageUrl = nofile
        End If

    End Sub

Open in new window


That should work unless I've introduced a new bug somewhere. :)

In regards to your messagebox function, try replacing strMsg in line 6 with strMsg.Replace("\", "\\") Javascript needs a double \ to display a \. It's called escaping the \. See http://www.quackit.com/javascript/tutorial/javascript_escape_characters.cfm
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question