This is a simple way of posting files from Android.
String url = "http://yourserver.com/upload.php";
File file = new File("myfileuri");
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(file), -1);
reqEntity.setContentType("binary/octet-stream");
reqEntity.setChunked(true); // Send in multiple parts if needed
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
//Do something with response...
} catch (Exception e) {
e.printStackTrace();
}
What I want to do is add more POST
variables to my request. How do I do that? While uploading plain strings in POST
request, we use URLEncodedFormEntity
.
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
Whereas while uploading files, we use InputStreamEntity
.
Also, how do I specifically upload this file to $_FILES['myfilename']
?
the most effective method is to use android-async-http
You can use this code to upload file :
File myFile = new File("/path/to/file.png");
RequestParams params = new RequestParams();
try {
params.put("profile_picture", myFile);
} catch(FileNotFoundException e) {}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With