Markus
Markus

Reputation: 45

ionic2 cordova File Transfer plugin time out error code 3

It seems no matter what I do on iOS my file transfer times out. I don't have an android device to test on currently but it was working as of yesterday. I have the file transfer plugin and whitelist installed.

Ionic Framework Version: 2.0.0-beta.8, ionic CLI Version: 2.0.0-beta.30, Ionic App Lib Version: 2.0.0-beta.16 Xcode version: Xcode 7.3.1

Here is my code:

upload = (image: string) : void => { 
  let ft = new Transfer();
  let filename = image.substr(image.lastIndexOf('/')+1)+'.jpg';
  let options = {
    fileKey: 'file',
    fileName: filename,
    mimeType: 'image/jpeg',
    chunkedMode: false,
    headers: {
        'Content-Type' : undefined
    },
    params: {
        fileName: filename,
        apikey: "mykey"
    }
  }; 
  ft.upload(image, "https://api.ocr.space/parse/image", options, false)
  .then((result: any) => {
    this.success(result);
  }).catch((error: any) => {
    this.failed(error);
  }); 
}

It times out everytime and gives this result:

[6311:2681244] -[CDVFileTransfer requestForUploadCommand:fileData:]

[Line 224] fileData length: 68706

[6311:2682532] FileTransferError {
    body = "";
    code = 3;
    "http_status" = 0;
    source = "file:///var/mobile/Containers/Data/Application/372A03D7-653A-45D8-B59A-EA34252E4AF3/tmp/cdv_photo_006.jpg";
    target = "https://api.ocr.space/parse/image";
}

[6311:2682532] File Transfer Error: The request timed out.

Upvotes: 0

Views: 1562

Answers (2)

Ferdy Fauzi
Ferdy Fauzi

Reputation: 71

As for iOS 9 and above, you need to add AppTransportSecurity in XCode before you publish to allow connection with HTTP. At XCode, add some key in info.plist. The steps I followed are:

  1. Opened my Projects info.plist file
  2. Added a Key called NSAppTransportSecurity as a Dictionary.
  3. Added a Subkey called NSAllowsArbitraryLoads as Boolean and set its value to YES. Or change info.plist with code as below:

You need to change yourserver.com with your own URL for file transfer

<key>NSAppTransportSecurity</key>
<dict>
  <key>NSExceptionDomains</key>
  <dict>
    <key>yourserver.com</key>
    <dict>
      <!--Include to allow subdomains-->
      <key>NSIncludesSubdomains</key>
      <true/>
      <!--Include to allow HTTP requests-->
      <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
      <true/>
      <!--Include to specify minimum TLS version-->
      <key>NSTemporaryExceptionMinimumTLSVersion</key>
      <string>TLSv1.1</string>
    </dict>
  </dict>
</dict>

Upvotes: 1

Markus
Markus

Reputation: 45

It appears to have been an issue with the endpoint. Here is a copy of the woking upload code for anyone looking.

upload = (image: string) : void => { 
let ft = new Transfer();
let options = new FileUploadOptions();

options.fileKey="file";
options.fileName=image.substr(image.lastIndexOf('/')+1)+'.jpg';
options.mimeType="text/plain";

let params = new Object();
params.apikey = "helloworld";
options.params = params;

//ft.onProgress(this.onProgress);
ft.upload(image, "https://apifree2.ocr.space//parse/image", options, false)
.then((result: any) => {
    this.success(result);
}).catch((error: any) => {
    this.failed(error);
}); 


}
success = (result: any) : void => { 
  console.log(result);
}
failed = (err: any) : void => {
  let code = err.code;
  alert("Failed to upload image. Code: " + code);
}

Upvotes: 0

Related Questions