This post originated from an RSS feed registered with .NET Buzz
by Doug Thews.
Original Post: Psoting Files Using WebClient.UploadFile
Feed Title: IlluminatiLand
Feed URL: http://apps5.oingo.com/apps/domainpark/domainpark.cgi?client=netw8744&s=JETBRAINS.COM
Feed Description: A technology blog for people enlightened enough to think for themselves
I've seen quite a few questions on the boards int he past month regarding posting files using the UploadFile method of the WebClient class. So, I thought I'd post some information that may help answer some of the most common questions regarding posting files to a remote server using the HTTP POST command.
#1. When using the UploadFile method, you need to have a page on the destination web server that you post to (using HTTP POST). It's that web page that reads the file names (and other arguments), and saves the data to that server. So, just using UploadFile like this (without a file on that server) will not work.
#2. When using the UploadFile method as shown below:
myWebClient = New WebClient
httpResponseArray = myWebClient.UploadFile(uriString, "POST", LocalFileName)
You need to remember that the uriString has to be the complete URL AND web page that is going to accept the HTTP POST data. UploadFile will not work when just specifying a directory, even if your web page is listed as the default page for that directory. So, if the page your calling is default.aspx, the uriString has to be: "http://www.myserver.com/default.aspx" and NOT "http://www.myserver.com/"
#3. You can use the BaseAddress property of the WebClient class to set a base address for all subsequent operations, and the specify the relative address from then on out.
#4. The byte stream returned from an UploadFile or UploadData request is actually the HTML returned from the page specified in uriString, and not a status of the transfer itself.
I'll try to add on things to this post as I catch them on the boards.