zoukankan      html  css  js  c++  java
  • ashx图片上传接收

    发送数据流方法
    
    /// <summary>
    /// PostBinaryData
    /// </summary>
    /// <param name="url">要发送的 url 网址</param>
    /// <param name="bytes">要发送的数据流</param>
    /// <returns></returns>
    public string PostBinaryData(string url, byte[] bytes)
    {
      //下面是测试例子
      //string url = "http://www.test.com/test.ashx";
      //string img = HttpContext.Current.Server.MapPath("../images/test.jpg");
      //byte[] bytes = File.ReadAllBytes(img);
      HttpWebRequest wRequest = (HttpWebRequest)WebRequest.Create(url);
      wRequest.ContentType = "multipart/form-data";
      wRequest.ContentLength = bytes.Length;
      wRequest.Method = "POST";
      Stream stream = wRequest.GetRequestStream();
      stream.Write(bytes, 0, bytes.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;
    }
    
    public void GetBinaryData()
    {
      string imgFile = DateTime.Now.ToString("yyyyMMddhhmmss") + ".jpg";
      string filePath = HttpContext.Current.Server.MapPath(imgFile);
      //方法一
      int lang = HttpContext.Current.Request.TotalBytes;
      byte[] bytes = HttpContext.Current.Request.BinaryRead(lang);
      string content = System.Text.Encoding.UTF8.GetString(bytes);
      FileStream fStream = new FileStream(filePath, FileMode.Create, FileAccess.Write);
      BinaryWriter bw = new BinaryWriter(fStream);
      bw.Write(bytes);
      bw.Close();
      fStream.Close();    
      //方法二
      Bitmap img = new Bitmap(HttpContext.Current.Request.InputStream);
      img.Save(filePath);
      HttpContext.Current.Response.Write("ok");
    }
    接收数据流方法
    

      

  • 相关阅读:
    LeetCode 217. 存在重复元素
    LeetCode 48. 旋转图像
    LeetCode 35. 搜索插入位置
    LeetCode 27. 移除元素
    LeetCode 145. 二叉树的后序遍历 (用栈实现后序遍历二叉树的非递归算法)
    LeetCode 328. 奇偶链表
    LeetCode 160. 相交链表 (找出两个链表的公共结点)
    LeetCode 26. 删除排序数组中的重复项
    LeetCode 836. 矩形重叠
    数据库
  • 原文地址:https://www.cnblogs.com/XuPengLB/p/8459533.html
Copyright © 2011-2022 走看看