C# - WebClient.UploadFile没有接收POST数据

时间:2014-12-10 22:05:08

标签: c# post webclient

我正在尝试编写一个工具批量上传图片到我的网站,但我在接收(或发送)实际数据时遇到问题。

我将从一些C#代码开始,因为它应该解释我尝试做得更好的事情,我可以说清楚:

private bool Upload( string LocalFile, int ItemID, string Description, DateTime Date, string Photographer )
    {
        WebClient oWeb = new System.Net.WebClient(); 
        NameValueCollection parameters = new NameValueCollection();
        parameters.Add( "Type", "1" );
        parameters.Add( "ID", ItemID.ToString() );
        parameters.Add( "Desc", Description );
        parameters.Add( "Date", Date.ToString( "yyyy-MM-dd" ) );
        parameters.Add( "Photographer", Photographer );

        oWeb.QueryString = parameters;
        var responseBytes = oWeb.UploadFile( "http://www.teamdefiant.co.uk/moveuploadedfile.php", LocalFile );
        string response = Encoding.ASCII.GetString(responseBytes);

        MessageBox.Show( "Response: \n\n" + response + "\n\nPost Data: \n\n" + LocalFile + "\n" + ItemID.ToString() + "\n" + Description + "\n" + Date.ToString("yyyy-MM-dd") + "\n" + Photographer );
        Clipboard.SetText( "Response: \n\n" + response + "\n\nPost Data: \n\n" + LocalFile + "\n" + ItemID.ToString() + "\n" + Description + "\n" + Date.ToString("yyyy-MM-dd") + "\n" + Photographer );

        return true;
    }

当我收到响应时(如MessageBox和Clipboard.SetText中所示),我得到以下数据:

Response:

Upload:
Type:
Size: 0kb
Stored in:







Post Data:

C:\Users\<MyFolder>\Pictures\Website\ExampleImage.png
4
Picture Description
2014-12-10
Photographer

为了完整起见,这里是php代码:

<?PHP
   if ($_FILES["myfile"]["error"] > 0)
   {
      echo "Error: " . $_FILES["myfile"]["error"] . "\n";
   }
   else
   {
      echo "Upload: " . $_FILES["myfile"]["name"] . "\n";
      echo "Type: " . $_FILES["myfile"]["type"] . "\n";
      echo "Size: " . ($_FILES["myfile"]["size"] / 1024) . " Kb\n";
      echo "Stored in: " . $_FILES["myfile"]["tmp_name"] . " \n";
      echo $_POST["Type"] . " \n";
      echo $_POST["ID"] . " \n";
      echo $_POST["Desc"] . " \n";
      echo $_POST["Date"] . " \n";
      echo $_POST["Photographer"] . " \n";
   }
?> 

我已经尝试过寻找答案,但我们无法找到一个我能理解的有效解决方案。 (我还在学习C#。)

我在Visual Studio中没有遇到任何代码错误,我没有在服务器端遇到任何POST错误,所以我很难过。

感谢任何和所有帮助!

修改

我尝试直接访问网页并获得相同的响应,所以看起来没有数据被发送到服务器? :(

1 个答案:

答案 0 :(得分:0)

你的c#前端代码没有什么东西

您需要定义内容类型八位字节流

Client.Headers.Add("Content-Type","binary/octet-stream");

您还需要在UploadFile中指定POST

oWeb.UploadFile ("http://www.teamdefiant.co.uk/moveuploadedfile.php","POST", LocalFile);

尝试我在互联网上找到的这个简单代码,创建一个不同的工作分支并尝试一下,这是一个更基本的工作条件:

php脚本:

<?php
$uploaddir = 'upload/'; // Relative Upload Location of data file

if (is_uploaded_file($_FILES['file']['tmp_name'])) {
$uploadfile = $uploaddir . basename($_FILES['file']['name']);
echo "File ". $_FILES['file']['name'] ." uploaded successfully. ";


if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully moved. ";
}

else
print_r($_FILES);
}

else {
echo "Upload Failed!!!";
print_r($_FILES);
}
?>

这是C#代码:

System.Net.WebClient Client = new System.Net.WebClient ();
Client.Headers.Add("Content-Type","binary/octet-stream");
byte[] result = Client.UploadFile ("http://your_server/upload.php","POST","C:\test.jpg");
string s = System.Text.Encoding .UTF8 .GetString (result,0,result.Length );