使用ionic将图像上传到服务器时,出现FileTransferError错误代码3

时间:2019-06-26 11:40:01

标签: typescript cordova ionic-framework

我已成功从图库中上传图像。但是上传图像时却被震撼

这是我的代码

 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++              
      }
  }

以下是我的错误输出

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"

以下是我的离子信息输出

离子:

离子CLI:5.1.0(C:\ Users \ pramo \ AppData \ Roaming \ npm \ node_modules \ ionic    离子框架:离子角3.9.2    @ ionic / app-scripts:3.2.4

科尔多瓦:

Cordova CLI:9.0.0(cordova-lib@9.0.1)    Cordova平台:Android 8.0.0,浏览器5.0.4    Cordova插件:cordova-plugin-ionic-keyboard 2.1.2,cordova-plugin-ionic-webview 1.2.1(和其他18个插件)

实用程序:

cordova-res:0.4.0    本机运行:0.2.6

系统:

NodeJS:v10.15.3(C:\ Program Files \ nodejs \ node.exe)    npm:6.4.1    作业系统:Windows 10

我的cordova插件是

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"

有人能指导我为什么会出现此错误并希望解决该错误吗?

4 个答案:

答案 0 :(得分:1)

尝试一下: 导入模型:

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


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

还尝试在您的config.xml文件中授予Internet权限。

答案 1 :(得分:0)

权限android.name.INTERNET不存在。在config.xml中使用它,或者也可以将其放入AndroidManifest.xml:

80

答案 2 :(得分:0)

谢谢大家的回答。 我解决了我的错误,但是方式大不相同。 首先,我在platform / android中找不到AndroidManifest.xml

相反,我在platform / android / android.json中找到了文件android.json

浏览文件的内容后,我发现以下代码:

"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
            }
          ]
        }
      }

请不要介意括号。

但是在这里,我编辑了代码并添加了:

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

这解决了我的问题

答案 3 :(得分:0)

也许许可不是这里的问题 我遇到了同样的问题,并通过添加读写权限来解决它

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

并安装

cordova plugin add cordova-plugin-whitelist
cordova prepare

并在您的config.xml中添加此行

<allow-navigation href="*" />