Thread: File upload
View Single Post
  #8 (permalink)  
Old January 9th, 2009, 09:39 AM
alliancejhall alliancejhall is offline
Friend of Wrox
Points: 788, Level: 10
Points: 788, Level: 10 Points: 788, Level: 10 Points: 788, Level: 10
Activity: 15%
Activity: 15% Activity: 15% Activity: 15%
 
Join Date: Nov 2007
Location: Central Florida, USA.
Posts: 199
Thanks: 2
Thanked 15 Times in 15 Posts
Default 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
Reply With Quote
The Following User Says Thank You to alliancejhall For This Useful Post:
bex (January 9th, 2009)