码迷,mamicode.com
首页 > Web开发 > 详细

Web Service(1):用Web Service实现客户端图片上传到网站

时间:2014-07-07 09:36:25      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   文件   

由于项目需要,通过本地客户端,把图片上传到网站.通过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     }

 

Web Service(1):用Web Service实现客户端图片上传到网站,布布扣,bubuko.com

Web Service(1):用Web Service实现客户端图片上传到网站

标签:des   style   blog   http   color   文件   

原文地址:http://www.cnblogs.com/liubeimeng/p/3820689.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!