Je dois télécharger un fichier sur mon serveur depuis un programme C#. Le problème est, je dois également POST deux chaînes en même temps. Jusqu'à présent, j'ai:C# Comment télécharger un fichier sur un serveur PHP?
HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create("http://localhost/test.php");
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "&name=Test";
postData += "&[email protected]";
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();
Voici le HTML et 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>
Je ne sais pas où ajouter le champ de fichier si: \ Toute aide serait appréciée!
Vérifiez la réponse ici http://stackoverflow.com/questions/566462/upload-files-with-httpwebrequest-multipart-form-data –