在php中从c#接收图像

时间:2011-08-26 11:54:38

标签: c# php

我使用此代码将桌面应用程序中的图像发送到服务器上的php文件:

// Create a request using a URL that can receive a post. 
WebRequest request = WebRequest.Create("http://example.com/img.php");
// Set the Method property of the request to POST.
request.Method = "POST"; 
// Create POST data and convert it to a byte array.
string postData = "hello";
byte[] byteArray = ImageToByte(image);//Encoding.UTF8.GetBytes (postData);
byteArray = Encoding.UTF8.GetBytes(postData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream ();
// Write the data to the request stream.
dataStream.Write (byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close ();
// Get the response.
WebResponse response = request.GetResponse ();
// Display the status.
Console.WriteLine (((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream ();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader (dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd ();
// Display the content.
Console.WriteLine (responseFromServer);
// Clean up the streams.
reader.Close ();
dataStream.Close ();
response.Close ();

我应该在php中使用什么功能来接收图像文件?

2 个答案:

答案 0 :(得分:1)

由于你没有形成标准化帖子,你需要直接从php输入中阅读。

file_get_contents("php://input");

答案 1 :(得分:0)

如果您的C#代码产生有效的发布请求,您应该能够从PHP中的全局$_POST数组中获取它。