C# 如何将文件上传到PHP服务器?

C# 如何将文件上传到PHP服务器?,c#,php,file,upload,field,C#,Php,File,Upload,Field,我需要从一个C程序上传一个文件到我的Web服务器。问题是,我需要同时发布两个字符串。 到目前为止,我已经: HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create("http://localhost/test.php"); ASCIIEncoding encoding = new ASCIIEncoding(); string post

我需要从一个C程序上传一个文件到我的Web服务器。问题是,我需要同时发布两个字符串。 到目前为止,我已经:

            HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create("http://localhost/test.php");             
            ASCIIEncoding encoding = new ASCIIEncoding();
            string postData = "&name=Test";
            postData += "&email=a@a.com";
            postData += "&file=file.txt";

            byte[] data = encoding.GetBytes(postData);

            HttpWReq.Method = "POST";    
            HttpWReq.ContentType = "application/x-www-form-urlencoded";   
            HttpWReq.ContentLength = data.Length;    
            Stream newStream = HttpWReq.GetRequestStream();


            newStream.Write(data, 0, data.Length);
            newStream.Close();
以下是HTML和PHP:

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
echo (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path) ? "Success!" : "Failed");
?>
<form enctype="multipart/form-data" action="test.php" method="POST">
Name : <input type="text" name="name"><br />
Email : <input type="text" name="email"><br />
File : <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Being Upload" />
</form>

名称:
电子邮件:
文件:

但我不知道在哪里添加文件字段:\n如有任何帮助,将不胜感激

可以尝试添加,或者在项目中使用CURL。

请注意,PHP文件中的HTML片段在
元素中正确地包含
enctype=“multipart/form data”
,当表单提交包含文件上载时,它必须使用multipart/form数据内容类型或其他multipart类型,但是在C代码中,您将HTTP请求
ContentType
设置为application/x-www-form-urlencoded。该内容类型不支持文件提交。上传的文件必须是提交表单时MIME消息中的一个单独部分


我不能提供代码示例,因为我一般不熟悉C#或.NET,但我相信应该有一个类来构造这样的消息,它可能与电子邮件处理有关。

好吧,无论如何,我不知道如何以相同的形式上传它们。例如,如果我使用FileUpload方法,它将上载文件,然后将其余文件提交到表单中。。。我想同时做这一切。。。哈,我不确定这是否有意义:\n在这里检查答案