|
|
 |
| ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 2.0 Professional section of the Wrox p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.
|
 |

January 8th, 2009, 03:08 AM
|
|
Friend of Wrox
|
|
Join Date: Aug 2008
Location: london, , United Kingdom.
Posts: 145
Thanks: 6
Thanked 1 Time in 1 Post
|
|
File upload
hi there i have this code
Code:
ProtectedSub Button1_Click(ByVal sender AsObject, ByVal e As System.EventArgs) Handles btnUpload.Click
If FileUpload1.HasFile Then
Try
FileUpload1.SaveAs("C:\test" & _
FileUpload1.FileName)
Catch ex As Exception
FileUploadReport.Text = "Faild Because:<br/>" & ex.Message
EndTry
FileUploadReport.Text = "File uploaded : <br/>" & FileUpload1.PostedFile.FileName
Else
FileUploadReport.Text = "Please select a file before clicking" & " the 'Upload' button"
EndIf
i need to add an exeption that if file exsists in the directory lbl1.text="file exsists try onother"
__________________
bx
|

January 8th, 2009, 07:35 AM
|
|
Authorized User
|
|
Join Date: Dec 2008
Posts: 16
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
how abt this:
Code:
if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } else { if (!File.Exists(path)) {...// u create the file save it hereon else { Response.Write(""); }
..if a directory has already been created it chks if the path xsits ..it self xplanatory..
__________________
Yamini
|

January 8th, 2009, 09:55 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Location: Central Florida, USA.
Posts: 164
Thanks: 2
Thanked 13 Times in 13 Posts
|
|
You could do...
something like?
Code:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click
If FileUpload1.HasFile Then
Try
If System.IO.File.Exists("C:\test\" & FileUpload1.FileName) Then
lbl1.text = "file exsists try another"
Else
FileUpload1.SaveAs("C:\test" & _
FileUpload1.FileName)
End If
Catch ex As Exception
FileUploadReport.Text = "Faild Because:<br/>" & ex.Message
End Try
FileUploadReport.Text = "File uploaded : <br/>" & FileUpload1.PostedFile.FileName
Else
FileUploadReport.Text = "Please select a file before clicking" & " the 'Upload' button"
End If
End Sub
__________________
Jason Hall
|

January 8th, 2009, 04:33 PM
|
|
Friend of Wrox
|
|
Join Date: Aug 2008
Location: london, , United Kingdom.
Posts: 145
Thanks: 6
Thanked 1 Time in 1 Post
|
|
it is not working it does not give me an error but the lbl1.text = "file exsists try another"
does not work,
when i try to upload the same file i sr this
FileUploadReport.Text = "File uploaded : <br/>" & FileUpload1.PostedFile.FileName
__________________
bx
|

January 8th, 2009, 07:28 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Location: Central Florida, USA.
Posts: 164
Thanks: 2
Thanked 13 Times in 13 Posts
|
|
Didn't see this before....
Are you adding test to the beginning of the file name when saving it or are you trying to add the file to the test directory under c:\ ?
if you are adding test to the file name then change my code to be this...
If System.IO.File.Exists("C:\test" & FileUpload1.PostedFile.FileName) Then
otherwise
If System.IO.File.Exists("C:\test\" & FileUpload1.PostedFile.FileName) Then
should work?
__________________
Jason Hall
|
|
The Following User Says Thank You to alliancejhall For This Useful Post:
|
|

January 9th, 2009, 02:39 AM
|
|
Friend of Wrox
|
|
Join Date: Aug 2008
Location: london, , United Kingdom.
Posts: 145
Thanks: 6
Thanked 1 Time in 1 Post
|
|
No I am not adding test to the filename i just want to upload it under test directory under c:\
the actual code is like this:
Code:
ProtectedSub Button1_Click(ByVal sender AsObject, ByVal e As System.EventArgs) Handles btnUpload.Click
If FileUpload1.HasFile Then
Try
If System.IO.File.Exists("C:\test\" & FileUpload1.FileName) Then
FileUploadReport.Text = "file exsists try another"
Else
FileUpload1.SaveAs("C:\test\" & FileUpload1.FileName)
EndIf
Catch ex As Exception
FileUploadReport.Text = "Faild Because:<br/>" & ex.Message
EndTry
FileUploadReport.Text = "File uploaded : <br/>" & FileUpload1.PostedFile.FileName
Else
FileUploadReport.Text = "Please select a file before clicking" & " the 'Upload' button"
EndIf
__________________
bx
|

January 9th, 2009, 02:44 AM
|
|
Friend of Wrox
|
|
Join Date: Aug 2008
Location: london, , United Kingdom.
Posts: 145
Thanks: 6
Thanked 1 Time in 1 Post
|
|
No I am not adding test to the filename i just want to upload it under test directory under c:\
the actual code is like this:
Code:
ProtectedSub Button1_Click(ByVal sender AsObject, ByVal e As System.EventArgs) Handles btnUpload.Click
If FileUpload1.HasFile Then
Try
If System.IO.File.Exists("C:\test\" & FileUpload1.FileName) Then
FileUploadReport.Text = "file exsists try another"
Else
FileUpload1.SaveAs("C:\test\" & FileUpload1.FileName)
EndIf
Catch ex As Exception
FileUploadReport.Text = "Faild Because:<br/>" & ex.Message
EndTry
FileUploadReport.Text = "File uploaded : <br/>" & FileUpload1.PostedFile.FileName
Else
FileUploadReport.Text = "Please select a file before clicking" & " the 'Upload' button"
EndIf
But i do upload the file to a sql database, does that make any diference?
cos every time i click the upload button the database is updated and multiple file names exsist in the table, i need an exeption for that too?
__________________
bx
|

January 9th, 2009, 08:39 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Location: Central Florida, USA.
Posts: 164
Thanks: 2
Thanked 13 Times in 13 Posts
|
|
Nope...
I am assuming that by uploading to the database you mean you are storing info about the file to the database and not the actual file in binary. If that's the case (either way really) it doesn't matter how many times the file name occurs in the database because you aren't checking the file name in the database you are checking the file exists in the test directory. But i realized what I did wrong. I've tested this code and it works....
Code:
If FileUpload1.HasFile Then
Try
If System.IO.File.Exists("C:\test\" & System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName)) = True Then
FileUploadReport.Text = "file exsists try another"
Exit Sub
Else
FileUpload1.SaveAs("C:\test\" & System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName))
End If
Catch ex As Exception
FileUploadReport.Text = "Faild Because:<br/>" & ex.Message
End Try
FileUploadReport.Text = "File uploaded : <br/>" & FileUpload1.PostedFile.FileName
Else
FileUploadReport.Text = "Please select a file before clicking" & " the 'Upload' button"
End If
When using FileUpload1.PostedFile.FileName the filename is actually the entire path to the file so when checking if that file exists it won't work. Example: You browse for a file located at C:\Upload\Test.txt and you upload it the code was checking C:\Test\C:\Upload\Test.txt instead of C:\Test\Test.txt. So you should use System.IO.Path.GetFileName() and that will strip off the Directory path and give you just Test.txt
I hope that all makes sense...
__________________
Jason Hall
|
|
The Following User Says Thank You to alliancejhall For This Useful Post:
|
|

January 9th, 2009, 11:59 AM
|
|
Friend of Wrox
|
|
Join Date: Aug 2008
Location: london, , United Kingdom.
Posts: 145
Thanks: 6
Thanked 1 Time in 1 Post
|
|
that did it thanks
__________________
bx
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |