I want user to upload pdf to server and make it downloadable to other user.I want to save the url of uploaded file to databse and then pickup that url to display the other user. I am learning android , so i dont have much idea . here is my code , i had tried to do it using multipart:-
public class Upload extends Activity {
String path=null;
private String getpath(String path){
return path;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.upload);
Button browse = (Button) findViewById(R.id.browse);
Button upload = (Button) findViewById(R.id.upload);
browse.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent();
intent.setType("application/pdf");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select PDF"), 1);
}
});
upload.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
upload up = new upload();
up.execute();
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent result) {
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
Uri data = result.getData();
String pathe = data.getPath();
path = getpath(pathe);
Toast.makeText(Upload.this, path, Toast.LENGTH_SHORT).show();
}
}
}
private class upload extends AsyncTask<Void, Void, Void>{
ProgressDialog pd;
@Override
protected void onPreExecute() {
pd= ProgressDialog.show(Upload.this, "Uploading", "Please Wait");
super.onPreExecute();
}
@Override
protected void onPostExecute(Void result) {
pd.dismiss();
super.onPostExecute(result);
}
@Override
protected Void doInBackground(Void... params) {
//Toast.makeText(Upload.this, path,Toast.LENGTH_LONG).show();
String url = "http://192.168.43.50/projectpri/upload.php";
File file = new File(path);
file.getAbsolutePath();
try {
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION,HttpVersion.HTTP_1_1);
HttpPost httppost = new HttpPost(url);
MultipartEntity mpe = new MultipartEntity();
ContentBody cbfile =new FileBody(file);
mpe.addPart("file", cbfile);
httppost.setEntity(mpe);
HttpResponse response = httpclient.execute(httppost);
HttpEntity ent = response.getEntity();
System.out.println(response.getStatusLine());
} catch (Exception e) {
// show error
}
return null;
}
}