Making sure a file exists before uploading it

N

Nathan Sokalski

I have a form that allows the user to upload a file. Even though <input
type="file" runat="server"> is intended to have the user choose the file
using the browse button, it still allows them to change the path before it
is uploaded. I currently use the following code to upload the file:
Dim upfilename As String = ""

If fileDetails.Value <> "" Then

Dim dir As String() =
fileDetails.PostedFile.FileName.Split("\".ToCharArray())

upfilename = dir(dir.GetUpperBound(0))

fileDetails.PostedFile.SaveAs(Server.MapPath("/") & upfilename)

End If

I use an If statement to make sure something is there, but I want to make
sure the file that it refers to is actually there before trying to perform
the upload. How can I do this? The fact that it is sometimes hard to look at
the client's hard drive (due to security reasons) I do not know how to make
sure the file actually exists, and I want to do everything possible to
prevent my users from seeing error messages. Thanks.
 




| I use an If statement to make sure something is there, but I want to make
| sure the file that it refers to is actually there before trying to perform
| the upload. How can I do this? The fact that it is sometimes hard to look
at
| the client's hard drive (due to security reasons) I do not know how to
make
| sure the file actually exists, and I want to do everything possible to
| prevent my users from seeing error messages. Thanks.

it really doesn't matter whether you check before-hand or when you actually
run your upload code that assumes it is a valid, file-containing
upload...either way, the user is going to, or should, be shown some kind of
error message to the effect that they are trying to do something that "just
ain't right". in fact, they would probably feel a little more assured if
they got confirmation of the upload when it was successful as well.

your desire to anticipate the user's experience is a very good aim. i think
pragmatism will win out here though, where confirmation of successful or
failing uploads that are adequately descriptive will be a good thing for the
user and a more straightforward programatic approach for you.

hth,

me
 
N

Nathan Sokalski

I do plan to tell the user that is was successful if it was, but either way
I have to have some sort of test because something that I will be doing
along with the upload is assigning the name of the file to a field in a
database, and I don't want to assign that field the name of a file that
doesn't exist. Therefore, I need some way to test whether the file exists so
I know whether or not to execute the database code. Thanks.
 
S

Stanav

'This function checks to see if a file exists, may be just what you need

Public Function FileExists(ByVal FileFullPath As String) As Boolean
Dim f As New IO.FileInfo(FileFullPath)
Return f.Exists
End Function

Stanav
 
S

Steve Walker

Stanav said:
'This function checks to see if a file exists, may be just what you need

Public Function FileExists(ByVal FileFullPath As String) As Boolean
Dim f As New IO.FileInfo(FileFullPath)
Return f.Exists
End Function

That would check whether a file exists on the server. In this case, I
think the requirement is to check that it exists on the client. You
can't do that from server-side code, the best you can do is as someone
else suggests, check the length of the upload stream.
 
C

Chris Dunaway

Your code will check to see if the file exists on the server. It is
also unnecessary.

Why duplicate a method that already exists in the IO namespace?

Why not just use:

If File.Exists(...) Then
...
End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,187
Latest member
RosaDemko

Latest Threads

Top