0

I am successfully in uploading image from gallery.But I am struck while uploading images

this is my code

 pictureUpload(x){ // x is file:///storage/emulated/0/Download/palakkeni.jpg
  if(this.network.noConnection()){
          this.network.showNetworkAlert()
      }else{
          let loading = this.loadingCtrl.create({
            spinner: 'bubbles',
            content: 'Uploading your Picture...'
          });
          loading.present();
          var fileArray = x.split("/");
          let len = fileArray.length;
          var file = fileArray[len - 1];
          let fileTransfer: FileTransferObject = this.transfer.create();;     
          let option: FileUploadOptions = {
            fileKey: 'img',
            fileName: x,
            mimeType: "multipart/form-data",
            headers: {
               authorization : 'e36051cb8ca82ee0Lolzippu123456*='
            },
            params: {
               name: file,
               id: this.empid
            }
          }
          this.completed = false;
          fileTransfer.upload(x, encodeURI("http://forehotels.com:3000/api/upload_employee_image"), option, true)
          .then((data) => {
             this.completed=true;
             loading.dismiss()
             console.log("image uploaded")
           }, (err) => {
             loading.dismiss()
             console.log(err)
          let alert = this.alertCtrl.create({
             title: err.text(),
             subTitle: err.json(),
             buttons: ['Dismiss'],
          });
          alert.present();
          });
        this.view_picture = file;
        this.picture=x
        this.uploaded++              
      }
  }

The following is my error output

FileTransferError
body: null
code: 3
exception: "Permission denied (missing INTERNET permission?)"
http_status: null
source: "file:///storage/emulated/0/Download/xyz.jpg"
target: "http://companydomainname.com:3000/api/upload_employee_image"

The following is my output of ionic info

Ionic:

Ionic CLI : 5.1.0 (C:\Users\pramo\AppData\Roaming\npm\node_modules\ionic) Ionic Framework : ionic-angular 3.9.2 @ionic/app-scripts : 3.2.4

Cordova:

Cordova CLI : 9.0.0 (cordova-lib@9.0.1) Cordova Platforms : android 8.0.0, browser 5.0.4 Cordova Plugins : cordova-plugin-ionic-keyboard 2.1.2, cordova-plugin-ionic-webview 1.2.1, (and 18 other plugins)

Utility:

cordova-res : 0.4.0 native-run : 0.2.6

System:

NodeJS : v10.15.3 (C:\Program Files\nodejs\node.exe) npm : 6.4.1 OS : Windows 10

My cordova plugins are

cordova-android-support-gradle-release 3.0.0 "cordova-android-support-gradle-release"

cordova-plugin-app-version 0.1.9 "AppVersion"

cordova-plugin-calendar 5.1.0 "Calendar"

cordova-plugin-contacts 3.0.1 "Contacts"

cordova-plugin-device 2.0.2 "Device"

cordova-plugin-file 6.0.1 "File"

cordova-plugin-file-transfer 1.7.1 "File Transfer"

cordova-plugin-filechooser 1.0.1 "File Chooser"

cordova-plugin-filepath 1.0.2 "FilePath"

cordova-plugin-geolocation 4.0.1 "Geolocation"

cordova-plugin-googleplus 5.3.0 "Google SignIn"

cordova-plugin-headercolor 1.0 "HeaderColor"

cordova-plugin-inappbrowser 3.0.0 "InAppBrowser"

cordova-plugin-ionic-keyboard 2.1.2 "cordova-plugin-ionic-keyboard"

cordova-plugin-ionic-webview 1.2.1 "cordova-plugin-ionic-webview"

cordova-plugin-nativestorage 2.3.1 "NativeStorage"

cordova-plugin-network-information 2.0.1 "Network Information"

cordova-plugin-splashscreen 5.0.2 "Splashscreen"

cordova-plugin-whitelist 1.3.3 "Whitelist"

cordova-plugin-x-socialsharing 5.4.0 "SocialSharing"

cordova-plugin-x-toast 2.6.2 "Toast"

cordova.plugins.diagnostic 4.0.7 "Diagnostic"

es6-promise-plugin 4.1.0 "Promise"

Can somebody please guide me as to why I am getting this error and hopefully solve it ?

  • Possible duplicate of [SecurityException: Permission denied (missing INTERNET permission?)](https://stackoverflow.com/questions/17360924/securityexception-permission-denied-missing-internet-permission) – Marius Jun 26 '19 at 15:40
  • If FileTransfer.upload() throwing error code 3 on Android. If the server is a Windows based server, try using another server. Also, don´t forget to add these lines: var options = new FileUploadOptions(); options.chunkedMode = false; options.headers = { Connection: "close" }; –  Jun 27 '19 at 09:54

4 Answers4

0

Try this: Import modelus:

import { FileTransfer, FileUploadOptions, FileTransferObject } from '@ionic-native/file-transfer';
import { File } from '@ionic-native/file';


imageUpload(imagePath) { // ImagePath is the File_URI you get from Camera Plugin. 
  const fileTransfer: FileTransferObject = this.transfer.create();
  let options: FileUploadOptions = {
      fileKey: 'image',
      fileName: '.png',
      chunkedMode: false,
      //mimeType: "image/jpeg",
    }
    fileTransfer.upload(imagePath, 'Server-URL', options)
      .then((data) => {
      console.log(data+" Uploaded Successfully");          
    }, (err) => {
      console.log(err);
    });
}

Also try to give Internet Permission in your config.xml file.

Najam Us Saqib
  • 3,190
  • 1
  • 24
  • 43
0

Permission android.name.INTERNET doesn't exist. Use this in config.xml or you can also put this into AndroidManifest.xml:

<uses-permission android:name="android.permission.INTERNET"/>
0

Thank You for the answers everyone. I solved my error, but in much different way. First of all ,I didn't find AndroidManifest.xml in platforms/android

I instead found a file android.json in platforms/android/android.json

Going through the contents of the file I found the following code:

"AndroidManifest.xml": {
        "parents": {
          "/manifest": [
            {
              "xml": "<uses-permission android:name=\"android.permission.READ_CALENDAR\" />",
              "count": 1
            },
            {
              "xml": "<uses-permission android:name=\"android.permission.WRITE_CALENDAR\" />",
              "count": 1
            },
            {
              "xml": "<uses-permission android:name=\"android.permission.GET_ACCOUNTS\" />",
              "count": 1
            },
            {
              "xml": "<uses-permission android:name=\"android.permission.USE_CREDENTIALS\" />",
              "count": 1
            }
          ],
          "/*": [
            {
              "xml": "<uses-permission android:name=\"android.permission.READ_CONTACTS\" />",
              "count": 1
            },
            {
              "xml": "<uses-permission android:name=\"android.permission.WRITE_CONTACTS\" />",
              "count": 1
            },
            {
              "xml": "<uses-permission android:name=\"android.permission.GET_ACCOUNTS\" />",
              "count": 1
            },
            {
              "xml": "<uses-permission android:name=\"android.permission.WRITE_EXTERNAL_STORAGE\" />",
              "count": 4
            },
            {
              "xml": "<uses-permission android:name=\"android.permission.READ_EXTERNAL_STORAGE\" />",
              "count": 1
            },
            {
              "xml": "<uses-permission android:name=\"android.permission.ACCESS_COARSE_LOCATION\" />",
              "count": 1
            },
            {
              "xml": "<uses-permission android:name=\"android.permission.ACCESS_FINE_LOCATION\" />",
              "count": 1
            },
            {
              "xml": "<uses-feature android:name=\"android.hardware.location.gps\" />",
              "count": 1
            },
            {
              "xml": "<uses-permission android:name=\"android.permission.ACCESS_NETWORK_STATE\" />",
              "count": 1
            },
            {
              "xml": "<uses-permission android:name=\"android.permission.BLUETOOTH\" />",
              "count": 1
            },
            {
              "xml": "<uses-permission android:name=\"android.permission.INTERNET\" />",
              "count": 1
            }
          ],
          "application": [],
          "/manifest/application": [
            {
              "xml": "<provider android:authorities=\"${applicationId}.sharing.provider\" android:exported=\"false\" android:grantUriPermissions=\"true\" android:name=\"nl.xservices.plugins.FileProvider\"><meta-data android:name=\"android.support.FILE_PROVIDER_PATHS\" android:resource=\"@xml/sharing_paths\" /></provider>",
              "count": 1
            }
          ],
          "/*/application": [
            {
              "xml": "<receiver android:enabled=\"true\" android:name=\"nl.xservices.plugins.ShareChooserPendingIntent\"><intent-filter><action android:name=\"android.intent.action.SEND\" /></intent-filter></receiver>",
              "count": 1
            }
          ]
        }
      }

Please don't mind the closing brackets.

But here I edited my code and added:

{
              "xml": "<uses-permission android:name=\"android.permission.INTERNET\" />",
              "count": 1
            }

This solved my problem

0

maybe permission just not the problem at here i've same issue and i solve it with add read and write permission

this.androidPermissions.checkPermission(this.androidPermissions.PERMISSION.READ_EXTERNAL_STORAGE).then(
  result => {
    if (!result.hasPermission) {
      this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.READ_EXTERNAL_STORAGE)
    }
  }
)
this.androidPermissions.checkPermission(this.androidPermissions.PERMISSION.WRITE_EXTERNAL_STORAGE).then(
  result => {
    if (!result.hasPermission) {
      this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.WRITE_EXTERNAL_STORAGE)
    }
  }
)

and install

cordova plugin add cordova-plugin-whitelist
cordova prepare

and add this line at your config.xml

<allow-navigation href="*" />
TIARWAY
  • 1
  • 1
  • 1