Web Service (1): Using Web Service to implement client image upload to Web site

Source: Internet
Author: User

Because the project needs, through the local client, the picture is uploaded to the website. Through WebService.

This is the client code:

1        Private voidBtnimg_click (Objectsender, EventArgs e)2         {3              This. Yanzheng ();4Mylocalhost. Mysoapheader MyHeader =NewMylocalhost. Mysoapheader ();///this is SoapHeader .5Mylocalhost. MyWebService MyService =NewMylocalhost. MyWebService ();//Invoke Service6Myservice.mysoapheadervalue =MyHeader;7 Openfiledialog1.showdialog ();8pictureBox1.Image =Image.FromFile (openfiledialog1.filename);9             stringName= This. textBox1.Text.ToString ();TenMyservice.createfiles (name, Photoimageinsert (pictureBox1.Image));//picture name, picture byte stream OneMessageBox.Show (Openfiledialog1.filename);//file Local Path AMessageBox.Show ("saved successfully"); -         } -  the         /// <summary> -         ///Convert Image object to byte[] byte stream -         /// </summary> -         /// <param name= "Imgphoto" >Image object, which is the uploaded image</param> +         /// <returns>byte[] Byte stream</returns> -          Public byte[] Photoimageinsert (System.Drawing.Image imgphoto) +         { A             //convert image to stream data and save as byte[] atMemoryStream Mstream =NewMemoryStream (); - Imgphoto.save (Mstream, System.Drawing.Imaging.ImageFormat.Png); -             byte[] Bydata =NewByte[mstream. Length]; -Mstream. Position =0; -Mstream. Read (Bydata,0, bydata.length); - Mstream. Close (); in             returnBydata; -}

Service-Side code:

1     /// <summary>2     ///How to generate picture files from the byte stream in the service3     /// </summary>4     /// <param name= "Imgname" >Picture name</param>5     /// <param name= "FormData" >Data Flow</param>6[System.Web.Services.Protocols.SoapHeader ("Header")]//SOAP headers for user authentication7[WebMethod (Description ="Create a picture", enablesession =true)]8      Public  voidCreatefiles (stringImgname,byte[] FormData)9     {Ten         //Picture Generation Path One         stringPath = HttpContext.Current.Server.MapPath ("") +@"/.. /jajaweixinqianduanweb/uploadcaipinimages/"+ Imgname +". jpg"; ABinaryWriter bw =NewBinaryWriter (file.create (Path, formdata.length, fileoptions.asynchronous)); -Bw. Write (FormData);//get the uploaded picture and save it - bw. Close (); the}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.