如何使用php和volley将pdf或word上载到服务器

时间:2018-06-21 10:59:16

标签: php android

我正在开发用于求职的android应用。因此,如何以PDF或word格式上传我的简历。我可以上传到正确的路径。但PDF无法使用。我已使用base 64方法将图像上传到服务器。在这里,我使用的是base 64方法。我是android和php中的新手。请帮助我,帮助将不胜感激。

用于选择文件

 private void getDocument()
    {
        Intent intent = new Intent();
        intent.setType("*/*");
        String[] extraMimeTypes = {"*/*"};
        intent.putExtra(Intent.EXTRA_MIME_TYPES, extraMimeTypes);
        intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select File"), PICK_PDF_REQUEST);
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if (requestCode == PICK_PDF_REQUEST && resultCode == RESULT_OK && intent != null && intent.getData() != null) {
            Uri   filePath = intent.getData();
            String uriString = filePath.toString();
            File myFile = new File(uriString);
            String path = myFile.getAbsolutePath();
             displayName = null;

            if (uriString.startsWith("content://")) {
                Cursor cursor = null;
                try {
                    cursor = getActivity().getContentResolver().query(filePath, null, null, null, null);
                    if (cursor != null && cursor.moveToFirst()) {
                        displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
                        BrowsCv.setText(displayName);
                    }
                } finally {
                    cursor.close();
                }
            } else if (uriString.startsWith("file://")) {
                displayName = myFile.getName();
                BrowsCv.setText(displayName);
            }
        }


        }

用于上传文件

 private void saveinformation() {

        pDialog = new ProgressDialog(getActivity());
        pDialog.setMessage("Saving.. Please wait...");
        pDialog.setCancelable(false);
        pDialog.show();





        StringRequest stringRequest = new StringRequest(Request.Method.POST, "https://alot.ae/api/cv_upload.php" ,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String ServerResponse) {


                        pDialog.dismiss();
                        try {
                            JSONObject jsonObject = new JSONObject(ServerResponse);


                            if (jsonObject.getInt("success") == 0) {

                                Toast.makeText(getActivity(), jsonObject.getString("message"), Toast.LENGTH_LONG).show();

                            } else if (jsonObject.getInt("success") == 1) {
                                Toast.makeText(getActivity(), jsonObject.getString("message"), Toast.LENGTH_LONG).show();


                            } else

                                Toast.makeText(getActivity(), jsonObject.getString("message"), Toast.LENGTH_LONG).show();

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {

                        pDialog.dismiss();

                        Toast.makeText(getActivity(), "Some Error Occurred", Toast.LENGTH_LONG).show();
                    }
                }) {
            @Override
            protected Map<String, String> getParams() {

                // Creating Map String Params.
                Map<String, String> params = new HashMap<String, String>();
                prefs = getActivity().getSharedPreferences(Config.SHARED_PREF_NAME, Context.MODE_PRIVATE);
                String userid = prefs.getString("userId","");

                params.put("user_id",userid);
                params.put("attachment", displayName );



                return params;
            }

        };


        RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
        requestQueue.add(stringRequest);


    }

PHP代码

<?php
 if(isset($_POST['user_id']) && $_POST['attachment'] != '' ){

    include_once("db_connect.php");
    $userid = $_POST['user_id'];
    $cv = $_POST['attachment'];






 if($userid == '' || $cv == '' )
 {

die(json_encode(array("success"=>0,"message"=>"Empty ")));

 }else
 {




        $path1 = "user_cvs/".$userid."_1.pdf";
        $file = $userid."_1.pdf";

    $sql2 =$con->prepare("UPDATE candidate_profile SET attachment = '$file' WHERE user_id = '$userid'");


 if( $sql2->execute())
{

            file_put_contents('../../jobs/uploads/'.$path1,base64_decode($cv));

echo(json_encode(array("success"=>1,"message"=>"Data Added successfully!!")));
}
else
{
die("Something Error".$sql."<br>".mysqli_error($con));
}

 }

}else{

  die(json_encode(array("success"=>0,"message"=>"Empty Request Parameters..!!")));

}
mysqli_close($con);




 ?>

0 个答案:

没有答案