UWP C# BackgoundUploader not working

289 views Asked by At

SO I'm trying to upload a file to my webserver using C#, UWP, and the background uploader namespace. Here is my C# code:

private async Task JamCloud_Upload_MP3()
    {
        try
        {

        Uri uri = new Uri("http://www.example.com/upload/upload.php");
        var file = storagefile.file;

        BackgroundUploader uploader = new BackgroundUploader();
        uploader.SetRequestHeader("userfile", file.Name);

        UploadOperation upload = uploader.CreateUpload(uri, file);

            // Attach progress and completion handlers.
            await upload.StartAsync();
        //HandleUploadAsync(upload, true);
        }
        catch (Exception ex)
        {
            f.MessageBox("Upload Error: "+ ex);
        }
    }

So, by default the upload.method is POST. So Im not sure how to catch this POST on the server so I wrote a PHP file like I was catching a post from a html form post.

<?php

if($_SERVER['REQUEST_METHOD'] == 'POST'){

$uploaddir = '/home/example/public_html/upload/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile);
}

?>

It doesnt seem to be working.... can anyone help me?

1

There are 1 answers

1
Brizzler On BEST ANSWER

Figured it out.

  1. Changed the upload.method = "PUT"; //in the C# code default was POST

  2. Change the PHP script to:

    <?php
    if($_SERVER['REQUEST_METHOD'] == 'PUT'){
        /* PUT data comes in on the stdin stream */
        $putdata = fopen("php://input", "r");
    
        /* Open a file for writing */
        $fp = fopen($_SERVER['HTTP_USERFILE'], "w");
    
        /* Read the data 1 KB at a time
           and write to the file */
        while ($data = fread($putdata, 1024))
          fwrite($fp, $data);
    
        /* Close the streams */
        fclose($fp);
        fclose($putdata);
    }   
    ?>