记录下C#的图片数据流发送与接收,方便有用之人使用
1、发送数据流图片
public string PostBinaryData(string url)
{
//下面是测试例子 // //
string img = @"E: esta.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:
ewNewWorkwww.etcp.comETCP1.0.2ETCP.CRMETCP.CRM.WEBUploadparkimages" + imgnames); //本地测试使用地址
fs.Write(br.ReadBytes(len), 0, len);
fs.Close();
br.Close();
}
---现路径为测试为本地路径,测试通过。