Android C#webservice uploading files

Source: Internet
Author: User

Android side

Public String uploadheadimg (Bitmap bitmap,string fileName) {

String[] arg={"btyestring", "FileName"};
String[] val={bitmaptostring (bitmap), fileName};

return BD. Getwebreturnmodel ("UploadFile", Arg, Val);

}
Public String bitmaptostring (Bitmap Bitmap) {
Convert Bitmap to String

string string = null;

Bytearrayoutputstream Bstream = new Bytearrayoutputstream ();

Bitmap.compress (Compressformat.png, Bstream);

byte[] bytes = Bstream.tobytearray ();

String = Base64.encode (bytes);

return string;

}

Public Bitmap Stringtobitmap (string string) {

Convert a string to a bitmap type

Bitmap Bitmap = null;

try {

Byte[] Bitmaparray;

Bitmaparray = Base64.decode (string);

Bitmap = Bitmapfactory.decodebytearray (Bitmaparray, 0,

Bitmaparray.length);

} catch (Exception e) {

E.printstacktrace ();

}
return bitmap;

}

Service Rui

#region File Upload
[WebMethod (Description = "File Upload", EnableSession = True)]
public string UploadFile (String btyestring,string FileName)
{
Try
{
String path = Server.MapPath ("/view/upload/headimg");
byte[] bytes = convert.frombase64string (btyestring);
System.IO.MemoryStream ms = new System.IO.MemoryStream (bytes);
System.Drawing.Bitmap B = (System.Drawing.Bitmap) System.Drawing.Image.FromStream (MS);
Picture Save Path

String NewFileName = DateTime.Now.ToString ("YYYYMMDDHHMMSS") + "_" + FileName;
B.save (path + "/" + NewFileName);


return new Jsonmessage {Success = true, Data = "/view/upload/headimg" +newfilename, Message = "Operation Succeeded"}. ToString ();

}

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.