Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Android Code-</p> <pre><code>public class UploadFileToServer extends AsyncTask&lt;Object, String, Object&gt; { URL connectURL; String params; String responseString; String fileName; byte[] dataToServer; FileInputStream fileInputStream; private int serverResponseCode; private String serverResponseMessage; private static final String TAG = "Uploader"; public void setUrlAndFile(String urlString, File fileName) { Log.d(TAG,"StartUploader"); try { fileInputStream = new FileInputStream(fileName); connectURL = new URL(urlString); } catch(Exception e) { e.getStackTrace(); publishProgress(e.toString()); } this.fileName = fileName.getAbsolutePath()+".txt"; } synchronized void doUpload() { String lineEnd = "\r\n"; String twoHyphens = "--"; String boundary = "*****"; Log.d(TAG,"lv1"); try { Log.d(TAG,"doUpload"); publishProgress("Uploading..."); HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection(); conn.setDoInput(true); conn.setDoOutput(true); conn.setUseCaches(false); conn.setRequestMethod("POST"); conn.setRequestProperty("Connection","Keep-Alive"); conn.setRequestProperty("Content-Type","multipart/form-data;boundary=" + boundary); DataOutputStream dos = new DataOutputStream(conn.getOutputStream()); dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes("Content-Disposition:form-data; name=\"Uploaded\";filename=\"" + fileName + "\"" + lineEnd); dos.writeBytes(lineEnd); Log.d(TAG,"LvA"); Log.d(TAG,twoHyphens + boundary + lineEnd + ";Content-Disposition:form-data; name=\"Uploaded\";filename=\"" + fileName + "\"" + lineEnd); int bytesAvailable = fileInputStream.available(); int maxBufferSize = 1024; int bufferSize = Math.min(bytesAvailable, maxBufferSize); byte[] buffer = new byte[bufferSize]; int bytesRead = fileInputStream.read(buffer,0, bufferSize); Log.d(TAG,"LvB"); while(bytesRead &gt; 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); fileInputStream.close(); dos.flush(); InputStream is = conn.getInputStream(); int ch; Log.d(TAG,"LvC"); StringBuffer buff = new StringBuffer(); while((ch=is.read()) != -1) { buff.append((char)ch); } // publishProgress(buff.toString()); dos.close(); // Responses from the server (code and message) serverResponseCode = conn.getResponseCode(); serverResponseMessage = conn.getResponseMessage(); // Log.d(TAG,"Buffer "+buff.toString()); Log.d(TAG,"Server Response "+serverResponseMessage); } catch(Exception e) { e.getStackTrace(); publishProgress(e.toString()); } } @Override protected Object doInBackground(Object... arg0) { Log.d(TAG,"lv1a"); doUpload(); Log.d(TAG,"Uploading Completed! Path: "+connectURL); return null; } protected void onProgressUpdate(String... progress) { //this.info.setText(progress[0]); Log.d("Progress", progress[0]); } } </code></pre> <p>PHP Code-</p> <pre><code>&lt; ?php $file_path = "uploads/"; $file_path = $file_path . basename( $_FILES['uploaded_file']['name']); if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) { echo "success"; } else{ echo "fail"; } ? &gt; </code></pre> <p>Also you can check <a href="http://www.codepuppet.com/2013/03/26/android-uploading-a-file-to-a-php-server/" rel="nofollow">link 1</a> <a href="http://sunil-android.blogspot.in/2013/09/file-upload-on-php-server-in-android.html" rel="nofollow">link 2</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload