I upload an image in android. Currently, my code only downloads a file, but I also want to send some parameter. I try to follow
FileInputStream fileInputStream = new FileInputStream(sourceFile); URL url = new URL(upLoadServerUri); conn = (HttpURLConnection) url.openConnection(); // Open a HTTP connection to the URL conn.setDoInput(true); // Allow Inputs conn.setDoOutput(true); // Allow Outputs conn.setUseCaches(false); // Don't use a Cached Copy conn.setRequestMethod("POST"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("ENCTYPE", "multipart/form-data"); conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); conn.setRequestProperty("uploaded_file", fileName); dos = new DataOutputStream(conn.getOutputStream()); dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""+ fileName + "\"" + lineEnd); dos.writeBytes(lineEnd); //Sending data dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes("Content-Disposition: form-data; name=\"paramName\"" + lineEnd); dos.writeBytes(lineEnd); dos.writeBytes(globalUID);
and on the server side I use php. this is how i try to get this parameter
$param = $_POST["paramName"]; target_path1 = "./places_photos/" . $param;
But my current code is loading the file, but it is not sending parameters. How can I send parameters and how to get them on the server side?
Update
Currently, the image is saved in the places_photos
directory, which is mentioned in the $target_path1
variable. I want to save this image in the user directory, and this directory is called the user ID. But, unfortunately, I am not getting a server-side user id. How can I send the user ID to the server along with the file?
source share