Solved

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

Posted on 2013-05-20
11
496 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
  • 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now