【发布时间】:2010-12-03 16:11:58
【问题描述】:
我需要从 C# 程序将文件上传到我的网络服务器。问题是,我还需要同时发布两个字符串。 到目前为止,我有:
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>
我不知道在哪里添加文件字段:\ 任何帮助将不胜感激!
【问题讨论】: