C# - WebClient.UploadFile 未接收 POST 数据
本文关键字:POST 数据 WebClient UploadFile | 更新日期: 2023-09-27 18:33:56
我正在尝试编写一个工具将图像批量上传到我的网站,但是在接收(或发送)实际数据到服务器时遇到问题。
我将从一些 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;
}
当我收到响应(如消息框和剪贴板.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错误,所以我被难住了。
感谢任何和所有的帮助!
编辑
我尝试直接访问该网页并得到相同的响应,因此看起来没有数据被发送到服务器?? :(
你的 C# 前端代码没有很少的东西
您需要定义内容类型八位字节流
Client.Headers.Add("Content-Type","binary/octet-stream");
您还需要在上传文件中指定 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 );