Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 8th, 2009, 04:08 AM
bex bex is offline
Friend of Wrox
 
Join Date: Aug 2008
Location: london, , United Kingdom.
Posts: 154
Thanks: 7
Thanked 1 Time in 1 Post
Default 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
  #2 (permalink)  
Old January 8th, 2009, 08:35 AM
Authorized User
 
Join Date: Dec 2008
Posts: 18
Thanks: 1
Thanked 0 Times in 0 Posts
Default

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
  #3 (permalink)  
Old January 8th, 2009, 10:55 AM
Friend of Wrox
 
Join Date: Nov 2007
Location: Central Florida, USA.
Posts: 207
Thanks: 2
Thanked 15 Times in 15 Posts
Default 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

Follow me on Twitter @jhall2013
  #4 (permalink)  
Old January 8th, 2009, 05:33 PM
bex bex is offline
Friend of Wrox
 
Join Date: Aug 2008
Location: london, , United Kingdom.
Posts: 154
Thanks: 7
Thanked 1 Time in 1 Post
Default

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
  #5 (permalink)  
Old January 8th, 2009, 08:28 PM
Friend of Wrox
 
Join Date: Nov 2007
Location: Central Florida, USA.
Posts: 207
Thanks: 2
Thanked 15 Times in 15 Posts
Default 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

Follow me on Twitter @jhall2013
The Following User Says Thank You to alliancejhall For This Useful Post:
jminatel (January 9th, 2009)
  #6 (permalink)  
Old January 9th, 2009, 03:39 AM
bex bex is offline
Friend of Wrox
 
Join Date: Aug 2008
Location: london, , United Kingdom.
Posts: 154
Thanks: 7
Thanked 1 Time in 1 Post
Default

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
  #7 (permalink)  
Old January 9th, 2009, 03:44 AM
bex bex is offline
Friend of Wrox
 
Join Date: Aug 2008
Location: london, , United Kingdom.
Posts: 154
Thanks: 7
Thanked 1 Time in 1 Post
Default

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
  #8 (permalink)  
Old January 9th, 2009, 09:39 AM
Friend of Wrox
 
Join Date: Nov 2007
Location: Central Florida, USA.
Posts: 207
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

Follow me on Twitter @jhall2013
The Following User Says Thank You to alliancejhall For This Useful Post:
bex (January 9th, 2009)
  #9 (permalink)  
Old January 9th, 2009, 12:59 PM
bex bex is offline
Friend of Wrox
 
Join Date: Aug 2008
Location: london, , United Kingdom.
Posts: 154
Thanks: 7
Thanked 1 Time in 1 Post
Default

that did it thanks
__________________
bx
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Filter File Types in ASP.NET File Upload ramuis78 ASP.NET 2.0 Basics 2 May 31st, 2007 10:50 AM
Whole Folder upload(Multi file Upload) ramasamy_rams XML 1 September 9th, 2005 12:43 PM
File upload sunilwarrior Apache Tomcat 0 April 27th, 2005 11:17 AM
JSP file upload and delete file pandjie JSP Basics 0 January 29th, 2005 10:49 PM



All times are GMT -4. The time now is 05:06 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.