using System.Net;
WebClient cl = new WebClient();
try{
cl.UploadFile("http://" + ip + "/test.php", file);
}
catch(Exception e)
{
MessageBox.Show("Upload failed");
}
現在,你可以從PHP文件訪問的文件。在下面的例子中,我在服務器計算機上創建一個文件夾,移動到該文件夾中的文件。
<?php
//check whether the folder the exists
if(!(file_exists('C:/Users/dhanu-sdu/Desktop/test')))
{
//create the folder
mkdir('C:/Users/dhanu-sdu/Desktop/test');
//give permission to the folder
chmod('C:/Users/dhanu-sdu/Desktop/test', 0777);
}
//check whether the file exists
if (file_exists('C:/Users/dhanu-sdu/Desktop/test/'. $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
//move the file into the new folder
move_uploaded_file($_FILES["file"]["tmp_name"],'C:/Users/dhanu-sdu/Desktop/test/'.$_FILES["file"]["name"]);
}
?>
,你也可以從PHP服務器下載數據,並顯示它通過使用下面的代碼,在C#Web瀏覽器
WebClient cl = new WebClient();
try{
byte[] response = cl.DownloadData("http://" + ip +"/test.php");
webBrowser1.DocumentText = System.Text.ASCIIEncoding.ASCII.GetString(response);
}
catch(Exception e)
{
MessageBox.Show("Download failed");
}