title: Android :Upload large file with AsyncTask
body: <p>I'm tryin to upload an image to a server. The code is working with little image size like 163Ko , but when i try to upload big image with 10Mb ,it does not works. Can anyone explain me the source of the problem. </p>
  
  <p>Is this code don't alloing me to upload big files ?</p>
  
  <p>Thank you in advance .</p>
  
  <pre><code>public class UploadFileFroURL extends AsyncTask&lt;String, String, String&gt; {
  
      private String upLoadServerUri = "http://192.168.1.150:8080/UploadToServer.php";
  
  
      private OnTaskCompletedUpload listener;
  
      public UploadFileFroURL (OnTaskCompletedUpload listener){
          this.listener=listener;
      }
  
      public UploadFileFroURL() {
          // TODO Auto-generated constructor stub
      }
      @Override
      protected String doInBackground(String... content) {
  
          long size = uploadFile(content[0]);
  
          return " ";
      }
      public long uploadFile(String sourceFileUri)
      {   long actuaStartlTime = 0 ;
          double debutPaquet ;
          double[] tabTotalDebit= new double [10];
          String fileName = sourceFileUri;
          long TotalSize=0;
          HttpURLConnection comm = null;
          DataOutputStream dos = null;
          String lineEnd = "\r\n";
          String twoHyphens = "--";
          String boundary = "*****";
          int bytesRead,bytesReadBis,bytesAvailable,bufferSize;
          byte []buffer;
          int maxBufferSize =   10240 ;
          sourceFile = new File(sourceFileUri);
  
          if(!sourceFile.isFile())
          {
              //dialog.dismiss();
              Log.e("UploadFile", "Source File not Exist: "+ imagepath);
  
              return 0;
          }
          else
          {
              try
              {
                  FileInputStream fis = new FileInputStream(sourceFile);
                  URL url = new URL(upLoadServerUri);
  
                  comm = (HttpURLConnection)url.openConnection();
                  comm.setDoInput(true);
                  comm.setDoOutput(true);
                  comm.setUseCaches(false);
                  comm.setRequestMethod("POST");
                  comm.setRequestProperty("Connection", "Keep-Alive");
                  comm.setRequestProperty("ENCTYPE", "multipart/form-data");
                  comm.setRequestProperty("Content-Type", "multipart/form-data;boundary=" +boundary);
                  comm.setRequestProperty("uploaded_file", fileName);
                  dos = new DataOutputStream(comm.getOutputStream());
                  dos.writeBytes(twoHyphens + boundary + lineEnd);
                  dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\"" + fileName + "\"" + lineEnd);
                  dos.writeBytes(lineEnd);
                  bytesAvailable = fis.available();
                  TotalSize = bytesAvailable ;
                  bufferSize = Math.min(bytesAvailable, maxBufferSize);
                  buffer = new byte[bufferSize];
  
                  bytesRead = fis.read(buffer,0,bufferSize);
                  long global = System.currentTimeMillis();
                  debutPaquet = System.currentTimeMillis() ;
                  double totalTime =0;
  
                  while(bytesRead &gt; 0)
  
                  { dos.write(buffer,0,bufferSize);
                      bytesAvailable = fis.available();
                      bufferSize = Math.min(bytesAvailable, maxBufferSize);
                      bytesRead = fis.read(buffer,0,bufferSize);
                      publishProgress (bytesRead+"");
  
                  }
  
                  dos.writeBytes(lineEnd);
                  dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
                  serverResonseCode = comm.getResponseCode();
                  String serverResponseMessage = comm.getResponseMessage();
                  if(serverResonseCode == 200)
                  { String msg = "File Upload Completed. \n\n See uploaded file here : /var/www/uploads";
                  }
  
                  fis.close();
                  dos.flush();
                  dos.close();
              }
              catch(MalformedURLException e)
              {
  
                  e.printStackTrace();
  
                  Log.e("Upload file to server", "error: " + e.getMessage(), e);
              }
              catch(Exception e)
              {
  
                  e.printStackTrace();
  
                  Log.e("Upload file to server", "error: " + e.getMessage(), e);
              }
  
              return TotalSize;
          }
      }
      protected void onProgressUpdate(String... progress) {
      }
      protected void onPostExecute(String reslut) {
          Log.i("msg","Upload comlete. \n\n See Upload file here : : /var/www/uploads ");
      }
  }
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>This the php code in the server :</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>