Lazar Kukolj
Lazar Kukolj

Reputation: 706

upload video and variable to server?

UPDATE answer is here :) https://stackoverflow.com/a/23648537/6042879

i want to be able to upload my video and a variable to my server to use in the PHP script.

So far i can choose the video i want from my phone and upload it perfectly fine to the server but cant figure out exactly how to send the variable with the video. I can do them separately but it wont work if i combine them.

i use this code to use to upload variables:

//Uploads the product details
        try {//Try block is to see if the call to the database can work.
            URL url = new URL(ProductDetails_URL);//Create a new URL and put there variable "register_URL" into it.
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();//Create a httpConnection and open it
            httpURLConnection.setRequestMethod("POST");//Use the request method
            httpURLConnection.setDoOutput(true);

            OutputStream OS = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));

            String data = URLEncoder.encode("ProductOwnerEmail", "UTF-8") + "=" + URLEncoder.encode(ProductOwnerEmail, "UTF-8") + "&" +
          URLEncoder.encode(DescriptionPoint3, "UTF-8");

            bufferedWriter.write(data);
            bufferedWriter.flush();
            bufferedWriter.close();
            OS.close();

            InputStream IS = httpURLConnection.getInputStream();
            IS.close();

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) { //url.openConnection() catch statement
            e.printStackTrace();
        }

Video upload code:

try{
                FileInputStream fileInputStream = new FileInputStream(sourceFile);
                URL url = new URL(UploadVideo_URL);
                conn = (HttpURLConnection) url.openConnection();
                conn.setDoInput(true);
                conn.setDoOutput(true);
                conn.setUseCaches(false);
                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("myFile", selectedPath);
                dos = new DataOutputStream(conn.getOutputStream());

                dos.writeBytes(twoHyphens + boundary + lineEnd);
                dos.writeBytes("Content-Disposition: form-data; name=\"myFile\";filename=\"" + selectedPath + "\"" + lineEnd);
                dos.writeBytes(lineEnd);






                dos.writeBytes(twoHyphens + boundary + lineEnd);
                dos.writeBytes("Content-Disposition: form-data;      name=\"Details\";Email=\"" + ProductOwnerEmail + "\"" + lineEnd);
                dos.writeBytes(lineEnd);
                dos.write(ProductOwnerEmail.getBytes());
                dos.writeBytes(lineEnd);

                dos.writeBytes(twoHyphens + boundary + lineEnd);
                dos.writeBytes("Content-Disposition: form-data; name=\"Details\";KeyCode=\"" + ProductKeyCode + "\"" + lineEnd);
                dos.writeBytes(lineEnd);
                dos.write(ProductOwnerEmail.getBytes());
                dos.writeBytes(lineEnd);








                bytesAvailable = fileInputStream.available();
                Log.i("Huzza", "Initial .available : " + bytesAvailable);

                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                buffer = new byte[bufferSize];

                bytesRead = fileInputStream.read(buffer, 0, bufferSize);

                while (bytesRead > 0) {
                    dos.write(buffer, 0, bufferSize);
                    bytesAvailable = fileInputStream.available();
                    bufferSize = Math.min(bytesAvailable, maxBufferSize);
                    bytesRead = fileInputStream.read(buffer, 0, bufferSize);
                }

                dos.writeBytes(lineEnd);
                dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

                serverResponseCode = conn.getResponseCode();

                fileInputStream.close();
                dos.flush();
                dos.close();

            } catch (Exception e) {
                e.printStackTrace();
                return "Product upload failed";
            }

PHP code:

<?php

if($_SERVER['REQUEST_METHOD']=='POST'){
$file_name = $_FILES['myFile']['name'];
$file_size = $_FILES['myFile']['size'];
$file_type = $_FILES['myFile']['type'];
$temp_name = $_FILES['myFile']['tmp_name'];






$ProductOwnerEmail = $_FILES['Details']['Email'];
$ProductKeyCode = $_FILES['Details']['KeyCode'];

$NewDirectory = "/var/www/html/ProductVideos/" . $ProductOwnerEmail;

if (!file_exists($NewDirectory))
{
    mkdir($NewDirectory, 0777, true);
}






$location = "/var/www/html/ProductVideos/$ProductOwnerEmail/" .    $ProductKeyCode;//$NewDirectory . '/' . $file_name;

move_uploaded_file($temp_name, $location);
echo   "Uploaded!";
}else{
echo "Error";
}
?>

Upvotes: 0

Views: 108

Answers (2)

Adeel Turk
Adeel Turk

Reputation: 907

U can use Volley lib for this purpose. It also Keep your requests in a queue and easy to use http://developer.android.com/training/volley/index.html

Upvotes: 0

Joey Chong
Joey Chong

Reputation: 1500

After you code:

dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"myFile\";filename=\"" + SelectedPDF + "\"" + lineEnd);
dos.writeBytes(lineEnd);

Write something like below to send variables:

dos.writeBytes(LINE_END);

// Loop a list of variable that you want to send to server.
/*for (StringKeyValuePair pair : yourVariableList) {
    dos.writeBytes(TWO_HYPHENS + BOUNDARY + LINE_END);
    dos.writeBytes("Content-Disposition: form-data; name=\"" + pair.getKey()+  "\"" + LINE_END);
    dos.writeBytes(LINE_END);
    dos.write(pair.getValue().getBytes());
    dos.writeBytes(LINE_END);
}*/

dos.writeBytes(TWO_HYPHENS + BOUNDARY + LINE_END);
dos.writeBytes("Content-Disposition: form-data; name=\"ProductOwnerEmail\"" + LINE_END);
dos.writeBytes(LINE_END);
dos.write(ProductOwnerEmail.getBytes());
dos.writeBytes(LINE_END);

dos.writeBytes(TWO_HYPHENS + BOUNDARY + LINE_END);
dos.writeBytes("Content-Disposition: form-data; name=\"ProductKeyCode\"" + LINE_END);
dos.writeBytes(LINE_END);
dos.write(ProductKeyCode.getBytes());
dos.writeBytes(LINE_END);

Upvotes: 1

Related Questions