File upload from Android (via Multipart Form data)

Most of the time we have to upload our file on server from our Application but when we try to find a way we got all the different ways to do so that make us more confuse. Some says to use some library and some says not to. So here is a method to upload a file to the server via Http Multi-part form data, it doesn’t need any external library and is quiet efficient as well.

Through it you can have a put request to send you file with extra data like in my case I have to send an additional reference tag along with it. So lets start. Here I am supposing that you have a File Adapter and you have selected the file you have to send. Basically you have the URI of the file. If you don’t please follow theĀ  link.

So here are you. with this code you can send your file to server with extra form data.

. . .

Comments (5)

Add Your Comment

  • Android: Get an image from device - Mobikul
    […] AndĀ  you have the image you wanted. You can load this image by converting it in a Bitmap or anything you want to do with it. For uploading this file to server you can follow this link. […]
  • Sarith Nob
    Thank you very much!

    The code work for me but the file uploaded/file received in server contain only “my_reference_text” not content of my pdf file! Please help me with the issue!

    • aastha gupta
      Check the URI you are sending or give me some snippets of your code so I can check what’s wrong.
  • Sarith Nob
    What does function getDataFromInputStream(String, String) (in line 74) do? And how does it do?
    • aastha gupta
      Its a custom function I made to parse the result and get the value of a particular key. Its of not use as after you got result you can perform action specific to your need.
  • css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again
    Close