我正在开发一个WinForms应用程序,并希望将二进制图像数据发送到Web应用程序。它是如何工作的?
我编码了这个:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.AllowWriteStreamBuffering = true;
request.KeepAlive = true;
request.Credentials = System.Net.CredentialCache.DefaultCredentials;
var fc = GetFileContent(varsayilanResimGuid);
byte[] postBytes = fc.Dosya;
request.ContentLength = postBytes.LongLength;
Stream requestStream = request.GetRequestStream();
requestStream.Write(postBytes, 0, postBytes.Length);
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string content = new StreamReader(response.GetResponseStream()).ReadToEnd();
以及如何在 .ashx 中接收此二进制图像内容?
我问,我回答。
如何接收二进制图像:
Stream gelenResim = context.Request.InputStream;
if (gelenResim.Length == 0)
{
e.Resim = "/Content/EmlakDetayImaj/Noimages.jpg";
}
string guidim = Guid.NewGuid().ToString().Substring(0, 4);
var KaydetResimDosya = "/Content/EmlakDetayImaj/" + guidim + ".jpg";
using (FileStream fileStream = System.IO.File.Create(context.Server.MapPath("~/Content/EmlakDetayImaj/" + guidim + ".jpg"), (int)gelenResim.Length))
{
byte[] bytesInStream = new byte[gelenResim.Length];
gelenResim.Read(bytesInStream, 0, (int)bytesInStream.Length);
fileStream.Write(bytesInStream, 0, bytesInStream.Length);
}