zoukankan      html  css  js  c++  java
  • 通过数据流发送接收图片

    记录下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();
    }

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

  • 相关阅读:
    springboot ssm propertis 如何搭建多数据源动态切换
    发送验证码
    二维码生成
    文件上传 下载
    git拉代码报错
    通过url 下载文件
    原生JS实现挡板小球游戏
    深入浅出解析AJAX
    深入解析CSS3圆周运动
    JS递归原理
  • 原文地址:https://www.cnblogs.com/hjhd/p/3246140.html
Copyright © 2011-2022 走看看