用Web Service实现客户端图片上传到网站

由于项目需要,通过本地客户端,把图片上传到网站.通过webservice.

这是客户端代码:

 1        private void btnimg_Click(object sender, EventArgs e)
 2         {
 3             this.yanzheng();
 4             mylocalhost.MySoapHeader myheader = new mylocalhost.MySoapHeader();///这是soapheader
 5             mylocalhost.MyWebService myService = new mylocalhost.MyWebService();//调用服务
 6             myService.MySoapHeaderValue = myheader;
 7             openFileDialog1.ShowDialog();
 8             pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
 9             string name=this.textBox1.Text.ToString();
10             myService.CreateFiles(name, PhotoImageInsert(pictureBox1.Image));//图片名字,图片字节流
11             MessageBox.Show(openFileDialog1.FileName);//文件本地路径
12             MessageBox.Show("保存成功");
13         }
14 
15         /// <summary>
16         /// 把Image对象转化成byte[]字节流
17         /// </summary>
18         /// <param name="imgPhoto">Image对象,就是上传的那张图片</param>
19         /// <returns>byte[]字节流</returns>
20         public byte[] PhotoImageInsert(System.Drawing.Image imgPhoto)
21         {
22             //将Image转换成流数据,并保存为byte[]
23             MemoryStream mstream = new MemoryStream();
24             imgPhoto.Save(mstream, System.Drawing.Imaging.ImageFormat.Png);
25             byte[] byData = new Byte[mstream.Length];
26             mstream.Position = 0;
27             mstream.Read(byData, 0, byData.Length);
28             mstream.Close();
29             return byData;
30         }

服务端代码:

 1     /// <summary>
 2     /// 把服务中的字节流生成图片文件的方法
 3     /// </summary>
 4     /// <param name="imgName">图片名字</param>
 5     /// <param name="FormData">数据流</param>
 6     [System.Web.Services.Protocols.SoapHeader("header")]//用户身份验证的soap头 
 7     [WebMethod(Description = "生成图片", EnableSession = true)]
 8     public  void CreateFiles(string imgName, byte[] FormData)
 9     {
10         //图片生成路径
11         string path = HttpContext.Current.Server.MapPath("") + @"/../JajaWeixinQianduanWeb/UpLoadCaiPinImages/" + imgName + ".jpg";
12         BinaryWriter bw = new BinaryWriter(File.Create(path, FormData.Length, FileOptions.Asynchronous));
13         bw.Write(FormData);//得到上传的那张图片,并保存
14         bw.Close();
15     }
原文地址:https://www.cnblogs.com/liubeimeng/p/3820689.html