Solved

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

Posted on 2013-05-20
11
510 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 500 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

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

726 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