C# Form 上传图片

 string bline = "---------------------------" + DateTime.Now.Ticks.ToString("x");

            var wc = new WebClient();
            wc.Headers.Add("Content-Type", "multipart/form-data; boundary=" + bline);

            List<byte> bss = new List<byte>();

            bss.AddRange(Encoding.UTF8.GetBytes(String.Format("--" + bline + "\r\nContent-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}\r\n", "user_id", "249")));
            bss.AddRange(Encoding.UTF8.GetBytes(String.Format("--" + bline + "\r\nContent-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}\r\n", "content", "开始测试接口。")));
            bss.AddRange(Encoding.UTF8.GetBytes(String.Format("--" + bline + "\r\nContent-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}\r\n", "visible", "-1")));

            //图1
            {               
                bss.AddRange(Encoding.UTF8.GetBytes(String.Format("--" + bline + "\r\nContent-Disposition: form-data; name=\"{0}\"\r\n\r\n", "pic_1")));
                bss.AddRange(File.ReadAllBytes("d:\\1.jpg"));
                bss.AddRange(Encoding.UTF8.GetBytes("\r\n"));
            }
            //图2
            {
                bss.AddRange(Encoding.UTF8.GetBytes(String.Format("--" + bline + "\r\nContent-Disposition: form-data; name=\"{0}\"\r\n\r\n", "pic_2")));
                bss.AddRange(File.ReadAllBytes("d:\\2.jpg"));
                bss.AddRange(Encoding.UTF8.GetBytes("\r\n"));
            }
            bss.AddRange(Encoding.UTF8.GetBytes("--" + bline + "--\r\n"));

            byte[] rbs = wc.UploadData("接收地址", bss.ToArray());
            string s = Encoding.UTF8.GetString(rbs);

            Debug.WriteLine(s);

 

posted @ 2016-10-11 17:12  鞍山老菜鸟  阅读(1279)  评论(0编辑  收藏  举报