0

所以我正在尝试使用 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 表单帖子中捕捉一个帖子一样。

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

?>

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

4

1 回答 1

1

弄清楚了。

  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);
    }   
    ?>
    
于 2017-01-10T15:26:48.673 回答