Douglas Roos
Douglas Roos

Reputation: 613

Android Issue while trying to upload files to Apache Server

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?

Upvotes: 1

Views: 415

Answers (1)

Douglas Roos
Douglas Roos

Reputation: 613

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();
                }

Upvotes: 1

Related Questions