C# Text file upload to a php websever
Okay so there a few examples around, but they are really long winded and confusing
Basically I want to upload a file lets say test.txt from my application to my webserver, i have the PHP to handle the post already,
Is there a quick and easy way to do this?
开发者_StackOverflowIve tried using:
WebClient client = new WebClient();
string myFile = @"C:\Draft_3.txt";
client.Credentials = CredentialCache.DefaultCredentials;
client.UploadFile(@"http://myweb.com/projects/idl/Draft Results/RK/myFile", "PUT", myFile);
client.Dispose();
but it gives me a "A first chance exception of type 'System.Net.WebException'
occurred in System.dll" runtime error when running that code is there something i am missing?
Did you mean to use the "POST"
method instead of "PUT"
? POST is the usual PHP-upload mechanism. PUT is for content management/virtual disk use really. I've never even heard of PHP supporting PUT, because it's usually handled before PHP gets involved; by Apache/httpd.
According to MSDN that exception is usually caused by one of the following:
The URI formed by combining BaseAddress, and address is invalid.
-or-
fileName is null, is Empty, contains invalid characters, or does not exist.
-or-
An error occurred while uploading the file.
-or-
There was no response from the server hosting the resource.
-or-
The Content-type header begins with multipart.
I would check the server you are posting to and verify that it's getting the request at all and if it is, what response is it sending.
精彩评论