I get stuck at that place and unable to send doc file to php server. I am using this code.
Here is PHP code.
if($_SERVER['REQUEST_METHOD']=='POST'){
$image = $_POST['image'];
$name = $_POST['name'];
require_once('dbConnect.php');
$sql ="SELECT id FROM volleyupload ORDER BY id ASC";
$res = mysqli_query($con,$sql);
$id = 0;
while($row = mysqli_fetch_array($res)){
$id = $row['id'];
}
$path = "uploads/$id.doc";
$actualpath = "http://10.0.2.2/VolleyUpload/$path";
$sql = "INSERT INTO volleyupload (photo,name) VALUES ('$actualpath','$name')";
if(mysqli_query($con,$sql)){
file_put_contents($path,base64_decode($image));
echo "Successfully Uploaded";
}
mysqli_close($con);
}else{
echo "Error";
}
Here is Java code
private void showFileChooser() {
Intent intent = new Intent();
intent.setType("file/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),
PICK_IMAGE_REQUEST);
}
I called asynTask on upload button.
if (v == buttonUpload) {
// uploadImage();
new PostDataAsyncTask().execute();
}
A function calls in doInBackground is
private void postFile() {
try {
// the file to be posted
String textFile = Environment.getExternalStorageDirectory()
+ "/Woodenstreet Doc.doc";
Log.v(TAG, "textFile: " + textFile);
// the URL where the file will be posted
String postReceiverUrl = "http://10.0.2.2/VolleyUpload/upload.php";
Log.v(TAG, "postURL: " + postReceiverUrl);
// new HttpClient
HttpClient httpClient = new DefaultHttpClient();
// post header
HttpPost httpPost = new HttpPost(postReceiverUrl);
File file = new File(filePath.toString());
FileBody fileBody = new FileBody(file);
MultipartEntity reqEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("file", fileBody);
httpPost.setEntity(reqEntity);
// execute HTTP post request
HttpResponse response = httpClient.execute(httpPost);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
String responseStr = EntityUtils.toString(resEntity).trim();
Log.v(TAG, "Response: " + responseStr);
// you can add an if statement here and do other actions based
// on the response
}
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
The exception I get is that
java.io.FileNotFoundException: content:/com.topnet999.android.filemanager/storage/0F02-250A/test.doc: open failed: ENOENT (No such file or directory)
There is file in emulator - test.doc. Is there is any thing I miss in code, please help me. Or suggest a tutorial to upload pdf to php server.
Thanks In Advance.
What you have is a content provider path. Not a file system path. So you cannot use the File... classes.
Instead use
For the rest your php code does not make sense as there is no base64 encoding at upload. Further the $path and $actualpath parameters are not used and confusing. And you did not tell what your script should do.
The code below is not tested ( as is ) but is generally how one might handle the file upload - there is, as you will see, a debug statement in there. Try to send the file and see what you get ~ if all lokks ok, comment out that line and keep your fingers crossed.
Here is the solution of my question: - Here is code of php file - file.php
Put this file in htdoc folder of Xampp inside test named folder (if there is test folder already then ok, otherwise make a folder named "test"). And also create a folder named "images", in which uploaded file was saved.
Create function to select file from gallery
Inside onActivityResult function
This is asyncTask to upload file to server,
Call the asyncTask on button click after selecting the file from gallery.
Hope this will help you. Happy To Help and Happy Coding.