首页 > 代码库 > Web Service(1):用Web Service实现客户端图片上传到网站
Web Service(1):用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 =http://www.mamicode.com/ 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 = http://www.mamicode.com/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 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。