3
votes

Does anyone have any idea what might be the problem? For the last weeks i've been trying to figure out why my code works only on iOS and not on Android devices. Any idea would be highy appreciated since i run out of options.

On the android device it shows to send the file but on the server i don't recive any informations (nor the picture neither the rest of the parameters). On the other hand on iOS all works just fine, and the server also.

My code:

home.html:

<button ion-button color="dark" (click)="takePhoto()">Open camera </button>
<img [src]="imageURL" *ngIf="imageURL" />
<br>imageURL={{imageURL}}
<br><button ion-button color="dark" (click)="upload()">Upload</button>

home.ts:

@Component({
      selector: 'page-home',
      templateUrl: 'home.html'
})
export class HomePage {
    imageURL: any;
    myInput: string;
    cameraUrl: string;

    constructor(public navCtrl: NavController) {
    }

   takePhoto(){
       Camera.getPicture().then((imageData) => {
            this.imageURL = imageData;
       }, (err) => {
            console.log(err);
       });
   }
   upload(){
       var ft = new Transfer();
       var options = {
       fileKey: 'file',
       fileName: 'filename.jpg',
       params:{operatiune:'uploadpoza'}
   }
   ft.upload(this.imageURL,encodeURI("https://www.myserver.com/test.php"),options)
       .then((data) => {
            // success
            alert("image send:"+this.imageURL);
        }, (err) => {
          // error
          alert("err: "+JSON.stringify(err));
        })
    }
}

I've checked the errors in two ways:

Case 1:

I have 2 alerts in my uploaded code: a. a message in case of success: alert("image send:"+this.imageURL); b. a message for error: alert("err: "+JSON.stringify(err));

In my case, I noticed that only the first message appears, the one where the code is executed correctly.

Case 2:

I've connected my phone through cable to the laptop. To launch the app I typed in "Node.js-Command-Prompt" the command "ionic run Android". I've opened Chrome browser and typed in the address bar "chrome://inspect/#devices". Then select "INSPECT-option" for my app and popup the console. The console didn't show any error. Is this the right way to test it, or should I try something different?

I printed the object "data" received in case of success for "ft.upload" function. The result are:

data.response=
data.responseCode=200
data.bytesSent=411240

For me this means that information is sent to the server.

One more thing: - I've run different kinds of tests: sending information to both servers with HTTPS and HTTP - I've tried adding the trustAllHosts=true and in another test trustAllHosts=false

And every time the same problem: works on iOS but fails on android

2

2 Answers

17
votes

Try setting chunkedMode false in the config.

   var options = {
   chunkedMode: false,
   fileKey: 'file',
   fileName: 'filename.jpg',
   params:{operatiune:'uploadpoza'}
0
votes

Try this:

var onUploadSuccess = function (r) {
     clearCache();
     alert(JSON.stringify(r));      
}

var onUploadFailed = function (error) {
     alert(JSON.stringify(r));
}

var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = imageURI.substr(imageURI.lastIndexOf('/') + 1);
options.mimeType = "image/jpeg";

var params = new Object();
params.action = "some value";//=== if u want to send some other parameters

options.params = params;
options.chunkedMode = false;

var ft = new FileTransfer();

ft.upload(imageURI, SITE_URL+"upload.php", onUploadSuccess, onUploadFailed, options);