By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,304 Members | 3,174 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,304 IT Pros & Developers. It's quick & easy.

Making sure a file exists before uploading it

P: n/a
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("\".ToCharAr ray())

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.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/
Nov 19 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a

| 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
Nov 19 '05 #2

P: n/a
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.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"" <a@b.com> wrote in message news:eb****************@fe04.lga...
| 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

Nov 19 '05 #3

P: n/a
If you check for the fileDetails.PostedFile.ContentLength > 0 (instead of
fileDetails.Value as you did) you can tell whether the file exists or not. I
tried it in this sample: http://www.webswapp.com/demos/htmlinputfile.aspx

In any way this test has to be performed on the server after postback.

HTH
--
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Nathan Sokalski" wrote:
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("\".ToCharAr ray())

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.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

Nov 19 '05 #4

P: n/a
'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

"Nathan Sokalski" <nj********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
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("\".ToCharAr ray())

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.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

Nov 19 '05 #5

P: n/a
In message <#E**************@TK2MSFTNGP10.phx.gbl>, Stanav
<st*******@hotmail.com> writes
'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.

--
Steve Walker
Nov 19 '05 #6

P: n/a
Steve Walker <st***@otolith.demon.co.uk> wrote in
news:rp**************@otolith.demon.co.uk:
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.


You could do it through Javascript remoting or Client Side call backs
(asp.net 2.0). However the OP will require considerable javascript
knowledge.

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 19 '05 #7

P: n/a
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

Nov 19 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.