Lost on how you would upload a file in VB.net
-
Hi all, I've been researching how to upload a file to a web server. I've found lots of articles about it (most here at CodeProject), but I'm having some problems with my code. Everytime I run the code I get the following error: The remote server returned an error: (404) Not Found. My Code:
Dim responseArray As Byte() Try Dim WC As New System.Net.WebClient responseArray = WC.UploadFile("http://someserver/Uploads/upload.aspx", "POST", "\\myfile.doc") txtResponce.Text = "Check the file at " + Encoding.ASCII.GetString(responseArray) Catch ex As Exception MsgBox(ex.ToString) End Try
What I don't understand is what "upload.aspx" is there for. There is never any code to go with it, so I'm not sure what it's there for.(MSDN has this same example, no code to upload.aspx). I've taken the "upload.aspx" out and I get the same error. I've given the ASPNET and ISS user accounts read/write access to the folder, so I'm sure it's not a permission problem. Can ANYONE explain what I'm doing wrong?
-
Hi all, I've been researching how to upload a file to a web server. I've found lots of articles about it (most here at CodeProject), but I'm having some problems with my code. Everytime I run the code I get the following error: The remote server returned an error: (404) Not Found. My Code:
Dim responseArray As Byte() Try Dim WC As New System.Net.WebClient responseArray = WC.UploadFile("http://someserver/Uploads/upload.aspx", "POST", "\\myfile.doc") txtResponce.Text = "Check the file at " + Encoding.ASCII.GetString(responseArray) Catch ex As Exception MsgBox(ex.ToString) End Try
What I don't understand is what "upload.aspx" is there for. There is never any code to go with it, so I'm not sure what it's there for.(MSDN has this same example, no code to upload.aspx). I've taken the "upload.aspx" out and I get the same error. I've given the ASPNET and ISS user accounts read/write access to the folder, so I'm sure it's not a permission problem. Can ANYONE explain what I'm doing wrong?
upload.aspx would be the webpage that would accept your file. The 404 error means you are trying to communicate to a server (or a document on a server) that does not exist.
Check out the CodeProject forum Guidelines[^] The original soapbox 1.0 is back![^]
-
upload.aspx would be the webpage that would accept your file. The 404 error means you are trying to communicate to a server (or a document on a server) that does not exist.
Check out the CodeProject forum Guidelines[^] The original soapbox 1.0 is back![^]
Ok, I finally figured it out. There is a property called
Request.Files
, It holds all of the data on the files that are being uploaded to that page. You can then loop through it and save the files to the web server. Code for anyone interested:Partial Class _Default
Inherits System.Web.UI.PageProtected Sub Page\_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Me.Request.Files.Count <> 0 then Dim I As Integer For I = 0 To Me.Request.Files.Count - 1 Me.Request.Files.Item(I).SaveAs("C:\\some\\path\\Uploads\\" & Me.Request.Files.Item(I).FileName) Next End If End Sub
End Class
I'm not sure that's the best way to go about it, but it works! I think I may write up an article about this as I couldn't find this information easily ANYWHERE.