Phonegap Vue js Android将CAMERA图像上传到API服务器

时间:2017-09-07 12:58:27

标签: android cordova vue.js android-camera phonegap

我正在尝试上传Android相机拍摄的照片(cordova-plugin-camera)。我的代码是

takePicture () {
  navigator.camera.getPicture(result => {
    this.newUnit.addedPic = true
    this.newUnit.image = result
  }, error => {
    alert(error);
  },
  {
    sourceType : Camera.PictureSourceType.CAMERA,
    destinationType: Camera.DestinationType.FILE_URI,
    encodingType: Camera.EncodingType.JPEG,
  });
},

在我的 this.newUnit.addedPic 中,我得到了如下路径:

file:///storage/emulated/0/Android/data/.../1234.jpg

如何使用它将图片从移动应用上传到服务器? 在我的网页部分,我使用FormData上传图片。

我试图通过FileTransfer来做,但我收到错误代码1:

let win = r => {alert(`win`)}
    let fail = error => {
      alert("An error has occurred: Code = " + error.code);
      console.log("upload error source " + error.source);
      console.log("upload error target " + error.target);
    }
    var options = new FileUploadOptions();
    options.fileKey = "file";
    options.fileName = this.newUnit.image.substr(this.newUnit.image.lastIndexOf('/') + 1);
    options.mimeType = "image/jpeg";

    var params = {};
    params.name = "test";
    params.creator = 3;

    options.params = params;

    var ft = new FileTransfer();
    ft.upload(this.newUnit.image, encodeURI("http://myserver/api/v0/units/"), win, fail, options);

1 个答案:

答案 0 :(得分:0)

解决。问题是因为空头和http方法。

  var headers = { 'Authorization':`Token ${token}` };
  options.headers = headers;

  options.httpMethod= "POST";
  options.chunkedMode = true;`