1

i have a problem whiile trying to upload files to a WebServer.

this is the code i'm using:

File file = new File(fileUri.getPath());
try {
    HttpClient httpclient = new DefaultHttpClient();

    HttpPost httppost = new HttpPost(URL_connect);
    Log.e("subiendo archivo",fileUri.getPath());
    InputStreamEntity reqEntity = new InputStreamEntity(
            new FileInputStream(file), -1);
    reqEntity.setContentType("binary/octet-stream");
    reqEntity.setChunked(true); // Send in multiple parts if needed
    httppost.setEntity(reqEntity);
    HttpResponse response = httpclient.execute(httppost);
    //Do something with response...

} catch (Exception e) {
    e.printStackTrace();
    Toast toast1 = Toast.makeText(getApplicationContext(),"Error "+e, Toast.LENGTH_SHORT);
    toast1.show();
    Vibrator vibrator =(Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
    vibrator.vibrate(200); 
}

But there's no way, the file is not being uploaded to the server, i'm trying this on server side:

$file = $_FILES["file"];

$nombre_archivo = $_FILES['file']['name']; 
$tipo_archivo = $_FILES['file']['type']; 
$tamano_archivo = $_FILES['file']['size'];
$temporal = $_FILES['file']['tmp_name'];

move_uploaded_file($temporal, "../img/media/".$nombre_archivo);

And in the LogCat there's nothing concerning the upload, it just prints the Log.e("uploading file","name and path of the file").

What's the problem?

2
  • I get 145 http error with this code Commented Jun 13, 2014 at 2:38
  • Yes, i've added the answer and right code months ago, you can see it below. Commented Jun 14, 2014 at 3:50

1 Answer 1

1

Solved, instead using the code posted before i've used this:

following this info:

http://blog.tacticalnuclearstrike.com/2010/01/using-multipartentity-in-android-applications/

try {
                  MultipartEntity entity = new MultipartEntity();
                 Log.e("enviando", fileUri.getPath());
                  entity.addPart("reporte", new StringBody(reporte));
                  entity.addPart("usuarioID", new StringBody(user));
                  entity.addPart("archivo", new FileBody(file));
                  httppost.setEntity(entity);
                  HttpResponse response = httpclient.execute(httppost);
                } catch (ClientProtocolException e) {
                     e.printStackTrace();
                } catch (IOException e) {
                     e.printStackTrace();
                }
Sign up to request clarification or add additional context in comments.

1 Comment

Do you notice any problems when uploading 2mb files?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.