I want to upload file with C# client app and Nusoap webservice. How i can do this? I use Nusoap webservice for insert in database but for upload files i don't have any idea. Please help me. Thanks.
Asked
Active
Viewed 1,233 times
1 Answers
2
As I haven't any experience with NuSOAP, I shall answer with the best of my knowledge of uploading a file to a server running PHP, without using NuSOAP.
The following code will POST
the contents of a given file to a PHP page, as if it was sent via a standard HTML form.
public void UploadFile(string path) {
WebClient wc = new WebClient();
wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
Int64 numBytes = new FileInfo(path).Length;
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
Byte[] data = br.ReadBytes(Convert.ToInt32(numBytes));
br.Close();
fs.Close();
wc.UploadData("http://127.0.0.1/upload.php", "POST", data);
}
Edit: Here is the PHP that I used for this a while ago. It is potentially insecure and will always overwrite the same file every time a new one is being uploaded. You could try to work some dynamic-ness into this, along with some file checks for security sake... but you should also be able to use a modified PHP file intended for standard uploads (from a web form).
<?php
$fp = fopen('snap.jpg', 'wb');
fwrite($fp, file_get_contents('php://input'));
fclose($fp);
?>
Spooky
- 2,848
- 8
- 24
- 39
-
Hi and thanks about your answer. But whats content of upload.php? Is it similar to any upload form or not? – Hajitsu Jun 11 '12 at 04:04
-
Can you help me about uploading file in PHP ? – Hajitsu Jun 11 '12 at 17:17
-
Sorry, I was sleeping. I have edited my answer with the PHP that I used to store the file. Note that there are securer and better ways of doing this. – Spooky Jun 12 '12 at 01:02