简体   繁体   中英

Sending Image in Base64 String and receiving in webservice via kSoap

i am sending an image in byte[] and 3 strings to a webservice usingksoap but its not working for me , i am not sure where i am wrong, in sending image from Android and at receiving end, i am putting the code here kindly check it Here is how i am converting image to byte[] at client (Android) side

Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();  
myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream .toByteArray();
String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);

and here is the code where i am sending it to webservice via Ksoap

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("Name", name);
request.addProperty("Email", email);
request.addProperty("Picture", encoded );
request.addProperty("Date", date);

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport. call(SOAP_ACTION, envelope);

SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
String str = result.toString();

and here is the webMethod where i am receiving this soap envelop

  [WebMethod]
    public String PutFile(String Name, String Email, String Picture, String Date)
        {

String PictureByteString = Picture;
Image imgFromString = SaveByteArrayAsImage(PictureByteString);
DateTime.Now.ToShortDateString() + ".jpg"));
string serverpath = Server.MapPath("~/" + Email + "-" + DateTime.Now.ToShortDateString());
imgFromString.Save(serverpath, System.Drawing.Imaging.ImageFormat.Jpeg);
String Path = serverpath + ".Jpeg";


            return Name;
        }

private Image SaveByteArrayAsImage(string base64String)
{
byte[] bytes = Convert.FromBase64String(base64String);

image;
using (MemoryStream ms = new MemoryStream(bytes))
{
image = Image.FromStream(ms);
}

return image;

}

when i send data to webservice android LogCat shows me

java.io.IOException: HTTP request failed, HTTP status: 500

i think so which means that the data i am sending to webservice is not of correct type, so i tried to make String Picture to byte[] Picture in webmethod but result was same. I am not being able to figure out where i am wrong ...

Update: now sending image in a Base64 string and java exception is gone but the webmethod is still not converting that Base64 string into image...

This is how i did it.
parameter passed to function is Base64 string

public string SendImage(string data)
    {
        byte[] myarray = Convert.FromBase64String(data);
        MemoryStream memStream = new MemoryStream(myarray);
        Image myimage = Image.FromStream(memStream);
        myimage.Save("G:\\image.png", ImageFormat.Png);
        return "succeeded";
    }

This is working perfectly for me, Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM