【问题标题】:How to post a binary image from desktop app to ashx handler and receive it?如何将二进制图像从桌面应用程序发布到 ashx 处理程序并接收它?
【发布时间】:2012-04-27 10:40:13
【问题描述】:

我正在开发一个 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 中接收此二进制图像内容?

【问题讨论】:

标签: c# asp.net httpwebrequest httphandler binary-data


【解决方案1】:

我提问,我回答。
如何接收二值图像:

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);
}

【讨论】:

    猜你喜欢
    • 2012-04-30
    • 1970-01-01
    • 1970-01-01
    • 2015-04-29
    • 1970-01-01
    • 1970-01-01
    • 2012-10-14
    • 1970-01-01
    • 2012-02-02
    相关资源
    最近更新 更多