Problem with uploadfile

Brad

Member
Joined
Mar 26, 2007
Messages
12
Programming Experience
Beginner
Hi guys, this is my post post so i thought i would make it a constructive one.

I seem to be having issue with My.Commputer.Network.UploadFile.

PHP:
My.Computer.Network.UploadFile( _
        "C:\Desktop\latestnews.txt", _
        "http://Brads/Other/latestnews.txt", "****", "************", True, 100000, FileIO.UICancelOption.DoNothing)
I keep on recieving this error, "The request was aborted: The request was canceled." I thought it was because i was on Vista and the permisions etc but its not. I also tryed to upload a file with Try and End Try, that removes the error but doesnt upload the file.

I am completely stumped

Thanks, Brad
 
Last edited:
Why are you uploading to a HTTP address? Webhotels provide you with FTP that you upload the files through.

If this is your own test server, and you are just trying things out and don't want to set up a FTP server, you have to make a ASP.Net page that will function as a upload form, the http uploadfile makes a POST and the webpage will find the file in the Request.Files object.

With correct permission there is also the possibility to make upload with Http 1.1 PUT command if server is enabled for this. Doing this with System.Net.WebClient, the default is POST for Http resources and STORE for Ftp resources. My.Computer.Network doesn't seem to support setting the command.
 
Hi John, thanks for the reply, could you please show me some kind of source code so i can get a general idea of using POST/GET methods as this way is quite new to me.

Thanks, Brad
 
The POST/STORE mentioned is default commands sent for the request if the target is a HTTP or a FTP. It is how the request is built when you use the Upload methods and you don't have to worry about it unless you need something different from default behaviour (which it doesn't seem you need here).

Here is the PUT example, even you haven't told why you aren't using the standard FTP.
VB.NET:
Dim webc As New Net.WebClient
webc.Credentials = New Net.NetworkCredential("username", "password")
webc.UploadFile("http://address/filename", "PUT", "c:\filename")
 
Thanks John for the replys once again. When I try this code I keep getting another error 'The remote server returned an error: (405) Method Not Allowed.'. I am starting to become realy frustrated with the fact why it wont let me do this.

In response to your other question, the reason why I am not using standard FTP software is because it is for a friend and I don't want to give him any sort of FTP access or rights. My program has admin controls such as change text and then upload it as I am using streamreaders, because i dont know how to use databases(SQL) with VB.net yet.
 
The error doesn't surprise me, PUT is not normally enabled.

I wasn't saying you to use 'standard ftp software', I was saying that you should use the UploadFile method against that FTP address where you are allowed to upload. Both the address and user/pass is hidden in your compiled app so I don't see the problem with that.
 
Okay, I think I understand but I think I have tried every method I can think of with help from yourself. The ***'s that represent the username and pass are not hidden in my app but on here to protect it.
 
Here is the source for one of my forms..

Public Class Form6

Private Sub Form6_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim latest As New IO.StreamReader("C:\Desktop\latestnews.txt")
TextBox1.Text = latest.ReadToEnd
latest.Close()
End Sub

Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
Me.Close()
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
My.Computer.FileSystem.DeleteFile("C:\Desktop\latestnews.txt")
My.Computer.FileSystem.WriteAllText("C:\Desktop\latestnews.txt", TextBox1.Text, True)
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If My.Computer.Network.IsAvailable = True Then
Dim webc As New Net.WebClient
webc.Credentials = New Net.NetworkCredential("**", "****")
webc.UploadFile("http://googlel.com/Other/latestnews.txt", "POST", "C:\Desktop\latestnews.txt")
Else
MsgBox("You are not connected to the internet, please try again.")
End If
End Sub
End Class
 
But you will compile the app, without the source there will be a difficulty for someone reading it out of the exe.

You don't have to specify the method when target is a Http resource it defaults to POST. But your code won't work. Review my previous suggestions.
 
summarized:
option1: POST to webpage (Http), create webpage that you can upload to, it don't have to be viewed by any browser.
VB.NET:
Dim webc As New Net.WebClient
'NetworkCredential usually not used
webc.UploadFile("http://address/upload.aspx", "c:\filename")
The upload.aspx ASP.Net webpage then check the Request.Files to get the file and write it to a location.

option2: STORE to Ftp,
VB.NET:
Dim webc As New Net.WebClient
webc.Credentials = New Net.NetworkCredential("username", "password")
webc.UploadFile("ftp://address/filename", "c:\filename")
option3: PUT to Http (code above in post 5, not allowed you found out, and usually isn't)

All these options you compile the app to a executable .exe file that users normally cannot read the source code and then user cannot get the password.
 
Thank you so much for your persistance John, it works!

Quite dissapointed that you had to tell me the answer but i've learnt something new!

Thanks again
 
Back
Top