繁体   English   中英

从PHP中的C#接收图像

[英]Receive image from c# in 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中使用什么功能来接收图像文件?

由于您没有形成标准的帖子,因此您需要直接从php输入中阅读。

file_get_contents("php://input");

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM