通过数据流发送接收图片

记录下C#的图片数据流发送与接收,方便有用之人使用

1、发送数据流图片

public string PostBinaryData(string url)
{
//下面是测试例子 // //
string img = @"E:\test\a.jpg"; //
byte[] bytess = File.ReadAllBytes(img);
HttpWebRequest wRequest = (HttpWebRequest)WebRequest.Create(url);
wRequest.ContentType = "multipart/form-data";
wRequest.ContentLength = bytess.Length;
wRequest.Method = "POST";
Stream stream = wRequest.GetRequestStream();
stream.Write(bytess, 0, bytess.Length);
stream.Close();
HttpWebResponse wResponse = (HttpWebResponse)wRequest.GetResponse();
StreamReader sReader = new StreamReader(wResponse.GetResponseStream(), System.Text.Encoding.UTF8);
string str = sReader.ReadToEnd();
sReader.Close();
wResponse.Close();
return str;
}

 

2、接收数据流图片

public void ProcessRequest (HttpContext context) {
Stream resStream = context.Request.InputStream;
int len = (int)resStream.Length;
BinaryReader br = new BinaryReader(resStream);
string imgnames = "lt" + DateTime.Now.ToString("yyyyMMddhhmmss") + DateTime.Now.Millisecond.ToString() + ".jpg"; //文件名称暂定时间+jgp
FileStream fs;

//fs = File.Create(@"D:/CRM/Upload/parkimages/" + imgnames); 服务器使用地址
fs = File.Create(@"E:\newNewWork\www.etcp.com\ETCP1.0.2\ETCP.CRM\ETCP.CRM.WEB\Upload\parkimages\" + imgnames); //本地测试使用地址
fs.Write(br.ReadBytes(len), 0, len);
fs.Close();
br.Close();
}

 

---现路径为测试为本地路径,测试通过。

转载于:https://www.cnblogs.com/hjhd/p/3246140.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值