Я использую класс MyWebRequest для отправки информации из приложения c# на сервер и подключения к файлу php.
Это класс MyWebRequest
class MyWebRequest
{
private WebRequest request;
private Stream dataStream;
private string status;
public String Status
{
get
{
return status;
}
set
{
status = value;
}
}
public MyWebRequest(string url)
{
request = WebRequest.Create(url);
}
public MyWebRequest(string url, string method)
: this(url)
{
if (method.Equals("GET") || method.Equals("POST"))
{
request.Method = method;
}
else
{
throw new Exception("Invalid Method Type");
}
}
public MyWebRequest(string url, string method, string data)
: this(url, method)
{
string postData = data;
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
}
public string GetResponse()
{
WebResponse response = request.GetResponse();
this.Status = ((HttpWebResponse)response).StatusDescription;
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();
return responseFromServer;
}
}
В форме C#:
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Image Files(*.jpg; *.jpeg; *.bmp)|*.jpg; *.jpeg; *.bmp";
if (open.ShowDialog() == DialogResult.OK)
{
image.Image = new Bitmap(open.FileName);
try
{
Image image1 = Bitmap.FromFile(open.FileName);
image1 = resizeimage(image1, image.Size);
string saveimagename = editnumber.Text + "-" + propertycode.Text + ".bmp";
image1.Save(saveimagename, System.Drawing.Imaging.ImageFormat.Bmp);
string pathname = AppDomain.CurrentDomain.BaseDirectory + saveimagename;
MyWebRequest uploadrequest = new MyWebRequest("http://mywebsitename/csharp.php", "POST", "Metod=Uploadfiles&Uploadtype=image&Filename=" + saveimagename + "&Pathname=" + pathname);
string uploadreq = uploadrequest.GetResponse();
MessageBox.Show(uploadreq);
}
catch
{
// Error Massage.
}
}
В csharp. php file:
if ($_POST["Metod"] == 'Uploadfiles'){
if ($_POST["Uploadtype"] == 'image')
{
$uploadDirectory = "./uploads/";
$pathname = $_POST["Pathname"];
$fileName = $_POST["Filename"];
$uploadPath = $uploadDirectory . $fileName;
$didUpload = move_uploaded_file($pathname, $uploadPath);
if ($didUpload)
{
echo "Sucsess";
}
else
{
echo "Erro In Upload Imgae.";
}
}
}
Я проверяю размер в c#, но после загрузки формы и нажимаю upload btn:
показать: Erro In Upload Imgae. Массаж.
Я использую класс MyWebRequest в других формах и работаю правильно !!!