Search code examples
c#web-servicesnusoap

Upload zip file by Nusoap in PHP and C#


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.


Solution

  • 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);
    ?>