[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2013-05-20
11
Medium Priority
?
513 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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

656 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