• c#http post请求处理文件


    1、通过读取路径将文件转为byte[]

        

      //将一个file文件转化为byte[]
            public static byte[] GetBytesFromUrl1(string url)
            {
                byte[] b;
                FileWebRequest myReq = (FileWebRequest)WebRequest.Create(url);
                WebResponse myResp = myReq.GetResponse();
                Stream stream = myResp.GetResponseStream();
                //int i;
                using (BinaryReader br = new BinaryReader(stream))
                {
                    b = br.ReadBytes(1000000);
                    br.Close();
                }
                myResp.Close();
                return b;
            }
        //通过Url地址将文件转成byte[]
            public static byte[] GetBytesFromUrl(string url)
            {
                byte[] b;
                HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
                WebResponse myResp = myReq.GetResponse();
                Stream stream = myResp.GetResponseStream();
                //int i;
                using (BinaryReader br = new BinaryReader(stream))
                {
                    b = br.ReadBytes(500000);
                    br.Close();
                }
                myResp.Close();
                return b;
            }

     2、通过接口传递图片文件方法

         

                
    请求代码:
    string filePath = @"F: eTestimage12.jpg"; byte[] bt = GetBytesFromUrl1(filePath); string str = Convert.ToBase64String(bt);

            StringBuilder sb = new StringBuilder();
            sb.Append("filename=" + HttpUtility.UrlEncode("as", Encoding.UTF8));
            sb.Append("&fileimg=" + HttpUtility.UrlEncode(str, Encoding.UTF8));

    string res = HttpHelper.httpPost("接口地址", sb.ToString());

    接口地址接收代码:
     string str = Request["fileimg"] ?? "";
    string filename = Request["filename"]??"";
    byte[] byt = Convert.FromBase64String(str);

              FileStream fs = new FileStream(@"D:xxwebaabimages" + filename + ".jpg", FileMode.Create);
              BinaryWriter bw = new BinaryWriter(fs);
              //开始写入
              bw.Write(byt, 0, byt.Length);
              //关闭流
              bw.Close();
              fs.Close();

                   然后可以出来byt图片文件了 
  • 相关阅读:
    postgres 如何把多行数据,合并一行,返回json字符串
    linux 安装中文字体(生成图片中文乱码解决)
    postgis 自相交数据检测 修复
    C# Winform程序如何获取运行路径, 控制台也可以
    Excel: Access is denied
    change the theme in VS2005 or VS2008
    接下来的一点计划
    wordwrap, breakword
    T SQL + 正则表达式
    神奇的Css + DIV,滚动的Grid
  • 原文地址:https://www.cnblogs.com/wenghan/p/13896167.html
Copyright © 2020-2023  润新知