Upload Bitmap Image Using HTTP Multipart in Xamarin Android

In this post, we will take a look at what are the API’s Xamarin Android facilitates to support multipart upload. For the sake of simplicity, in this example, we will upload a bitmap image to the server.

The Multipart upload is a mechanism that enables you to upload large objects in chunks. In a mobile application context, it is useful when you capture a video or a photo using the device camera and want to upload it to your server.

In a multi-part request, each chunk/parts are separated by a delimiter. A delimiter is also known as a boundary that separates each part of a multi-part message. Each part of the message can define its own standard headers such as Content-Type and content disposition providing the name of the file whose value it contains. The multi-part delimiters and header fields are always 7-bit ASCII in any case.

For more detailed information on multi part content type search for RFC1341(MIME) or visit http://www.w3.org/

1. Convert the Bitmap to stream

Let us first convert the bitmap image into byte array to send it as ByteArrayContent via multi part form upload.

Notice that the bitmap.Compress() write a compressed version of the bitmap to the specified stream. The second integer argument indicates the compress quality ranging from 0 being low quality and to 100 maximum.

2. Add request headers

Add the following additional content header such as media content type and content disposition. The "my_uploaded_image.jpg" is the name of image sent to server.

A blogger, a bit of tech freak and a software developer. He is a thought leader in the fusion of design and mobile technologies. He is the author of Xamarin Mobile Application Development for Android Book (goo.gl/qUZ0XV3), DZone MVB and founder of stacktips.com.

Discussions

Please note: We reserve the right to delete comments that contains snarky remarks, offensive or off-topic. To know more read our comments policy.

500

2Comment threads

2Thread replies

0Followers

Most reacted comment

Hottest comment thread

500

newestoldestmost voted

Rambo Kids-Maringmei

send me the full source code plzz

Vote Up0Vote Down Reply

Sep 23, 2017 7:28 AM

Paul Völker

hey. thanks for sharing. but i am getting an error: system.net.webexception –> forbidden. i guess this is while i haven’t configured my IIS so far…whats the server site code to accept the upload?

Vote Up0Vote Down Reply

Sep 1, 2017 9:36 AM

Nilanchala Panigrahy

Hey @paulvlker:disqus Looks like server is blocking you. What is the response code you getting?

Vote Up0Vote Down Reply

Sep 1, 2017 11:14 AM

Paul Völker

the error has gone. i added the “write” flag on the system.webServer/handlers. i got no error now but the image is not in my directory?! where do i have to look into for further investigation? thanks, paul