File upload from Android (via Multipart Form data)

Save

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 confused. 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.

. . .
Discuss on Helpdesk

Leave a Comment

Your email address will not be published. Required fields are marked*


8 comments

  • 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.
  • Rizwan
    Thanks, It helped me!
  • fayzal
    I tried this code and the uploaded image missing exif data.
    • anchit (Moderator)
      Well, You need to re-apply the EXIF headers to the new OutputStream to preserve the EXIF Information.
  • css.php
    Start a Project


      Message Sent!

      If you have more details or questions, you can reply to the received confirmation email.

      Back to Home