How to pass binary or image file to a web service

Saturday, 21 August 2010 09:21 by sheel

Some time it is required to create a web service in which one method can take image or binary file data as a parameter.

So to achieve this we have to format or image or binary data to transferable format over HTTP.

Here i am going to explain how we can do this in asp.net or java web service. I am taking example of asp.net.

To do this simple use Convert.ToBas64String() and and Convert.FromBase64String() methods. In Client change binary/ image data to byte array and then change it to Base64 string and in web service change base64 string to byte array and save it or use it as you want.

 

Web service code

[WebMethod]

        public bool addImage(string fileName, string data)

        {

            byte [] bytes = Convert.FromBase64String(data);

            MemoryStream ms = new MemoryStream(bytes);

            Image img = System.Drawing.Image.FromStream(ms);

      img.Save(@"c:\temp\" + fileName, System.Drawing.Imaging.ImageFormat.Jpeg);

 

            return true;

        }

 

 

Client Code Snippet.

 

public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            ServiceReference1.WebService1SoapClient client = new ServiceReference1.WebService1SoapClient();

            Bitmap bmp = new Bitmap("d:\\temp\\DSC04618.jpg");

            MemoryStream mstream = new MemoryStream();

            bmp.Save(mstream, ImageFormat.Jpeg);

            byte []bytes = new byte[mstream.Length];

            mstream.Position = 0; 

            mstream.Read(bytes, 0, (int)mstream.Length);

            client.addImage("DSC04618.jpg",Convert.ToBase64String(bytes));

        }

    }

 

Comments

Comments (4) -

March 13. 2011 04:12

Lev

You need set imageStream.Position to 0. When you write to the MemoryStream it moves the Position after the bytes you just wrote so if you try to read there's nothing there
Add next line :
mstream.Position = 0;
before mstream.Read(bytes, 0, (int)mstream.Length);



Lev

March 13. 2011 18:08

admin

Yes Lev, you are correct. I am updating the same.
Thank you

admin

March 15. 2011 06:05

JC

Can I ask why you need to convert the byte array to a string?  Can't the web method simply take in byte[] data instead of string data?  If so then it would remove two conversions of the data.

JC

March 15. 2011 07:43

anupama

Dear JC,
There is no specific reason for conversion from byte[] to Base64 string.
SOAP uses XML, any binary data in the SOAP message will have to be encoded as text. This is usually done using Base64 encoding which increases the size of the binary data by 33%.

You are right it will require two conversions. But sending directly as raw data requires MTOM implementation. ASp.net web service implements it but not all java web service framework.
Base64 conversion will work with all.

anupama