[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 516
  • Last Modified:

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

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
bignadad
Asked:
bignadad
  • 6
  • 5
1 Solution
 
quizwedgeCommented:
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
 
bignadadAuthor Commented:
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
 
quizwedgeCommented:
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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
bignadadAuthor Commented:
its still not setting image.

messagebox(file) is the following
image
0
 
quizwedgeCommented:
All of the \'s are getting taken out and I'm not sure why. What happens if you do
messagebox(Server.MapPath(""))
0
 
bignadadAuthor Commented:
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
 
quizwedgeCommented:
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
 
bignadadAuthor Commented:
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
 
bignadadAuthor Commented:
okay so i tried this

Response.Write(file)

and here is the result

D:\Hosting\11100286\html\BAMEQ\1013\1.jpg
0
 
bignadadAuthor Commented:
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
 
quizwedgeCommented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now