• 通过数据流发送接收图片


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

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

  • 相关阅读:
    Linux
    Linux
    JavaScript
    JavaScript
    Linux
    不可不说的Java“锁”事
    RabbitMQ公共配置
    求一个数字的补码
    项目中Controller的全局异常处理类
    如何较方便给上百张数据库表添加表字段
  • 原文地址:https://www.cnblogs.com/hjhd/p/3246140.html
Copyright © 2020-2023  润新知