UWP C# BackgoundUploader 不工作

UWP C# BackgoundUploader not working

所以我正在尝试使用 C#、UWP 和后台上传程序命名空间将文件上传到我的网络服务器。这是我的 C# 代码:

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);
        }
    }

因此,默认情况下 upload.method 是 POST。所以我不确定如何在服务器上捕获这个 POST,所以我写了一个 PHP 文件,就像我从 html 表单 post 捕获 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);
}

?>

它似乎没有用....任何人都可以帮助我吗?

想通了。

  1. 更改了 upload.method = "PUT"; //在 C# 代码中默认为 POST

  2. 将 PHP 脚本更改为:

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