2017-04-18 91 views
1

在我的Ionic 2应用程序中,我使用cordova-plugin-camera拍摄一张照片,其中我检索的照片是FILE_URL(本地设备的图像位置),效果很好。FileTransfer返回所有属性为'null'的错误

这给我的网址,像这样拍摄图片的相机时:file:///storage/emulated/0/Android/....../1234.jpg

而且这样选择从库中的图像时:content://com.android.providers.media.documents/document/image%232312

现在我想要使用上传这个图片cordova-plugin-file-transfer

我上传的功能是这样的:

upload(){ 
    let options: FileUploadOptions = { 
     fileKey: 'file', 
     fileName: 'my_image.jpg', 
     headers: {'X-CSRF-Token': localStorage.getItem('u:token')} 
    } 
    alert(this.imageSrc); 
    this.fileTransfer.upload(this.imageSrc, encodeURI(this.API_URL), options, true) 
     .then(data => { 
      alert("d:"+JSON.stringify(data)); 
     }, err => { 
      alert("E:"+JSON.stringify(err)); 
     }); 
} 

但是我收到一个错误对象持有以下

{ 
    "code" : "null", 
    "source" : "null", 
    "target" : "null", 
    "http_status" : "null", 
    "body" : "null", 
    "exception" : "null" 
} 

注:没有额外的误差修改正在throwm

+0

你确定它不是一个服务器端的问题? –

+0

@suraj我期望一个'http_status'的值为'500',如果是这样的话,甚至是'404'如果它不存在 – Ivaro18

+0

true ..但类似cors的问题会给你null/0状态 –

回答

0

好的,所以问题在于画廊返回了contentURI而不是fileURI,这是FileTransfer需要的。问题出现在content:/storage/ URI上。

我使用了一个不好的练习解决方法,但由于它仍然不固定,我会分享它。

我通过添加FilePathcordova-plugin-filepath)来修复它。

下面的函数使用存储和内容URI并将它们转换为文件路径。

(图片来源在我的情况下全球)

convertIfAndroidGalleryImage() { 
     // android bug, sometimes returns an image starting with content: or /storage/ which won't upload. 
     // Convert to a filepath. 
     if(this.imageSrc.startsWith('content') || this.imageSrc.startsWith('/storage/')) { 
      if(this.imageSrc.startsWith('/storage/')) { 
       this.imageSrc = 'file://'+this.imageSrc; 
      } 
      this.filePath.resolveNativePath(this.imageSrc).then(filePath => { 
       this.imageSrc = filePath; 

      }).catch(err => { console.log("error occurred"); }); 
     } 
    } 
+0

附加信息:通常我根据上传的大小将我的FileURI转换为base64。 – Ivaro18

+0

我在做所有事情。我浪费了很多时间,为什么我甚至面临这个问题。我只是在没有--livereload的情况下在我的应用程序上尝试过它,它的作用就像一个魅力,所以不要使用--livereload运行此插件。在真实设备上运行它。 –

1

编辑:

在那里我已经在过去使用这个插件,我通常会传递一个:

file:///Path/To/File/ 

或者类似的东西:

cdvfile://localhost/persistent/path/to/file.txt 

您也可以通过在数据URI是这样的:

data:image/jpg;base64,iVBORw0KGgoAAA.... 

我看到你正在使用相机插件你试过设置相机选项来返回一个数据的URL和发送?

navigator.camera.getPicture(onSuccess, onFail, 
{ 
    destinationType: Camera.DestinationType.DATA_URL; 
}); 

原始

尝试构建你的选择是这样的:

var options = new FileUploadOptions(); 

options.fileKey = 'file', 
options.fileName = 'my_image.jpg', 
options.headers = {'X-CSRF-Token': localStorage.getItem('u:token')} 

然后尝试上传这样的:

var ft = new FileTransfer(); 

ft.upload('file_url', 'endpoint_url', success_callback, fail_callback, options); 
+0

这里有两个问题,1:''FileUploadOptions'只能引用一个类型,但在这里被用作值'; 2:不支持回调。 (Ionic修改了cordova插件我猜:https://ionicframework.com/docs/native/transfer/) – Ivaro18

+0

感谢您的链接。以前没见过。 –

+0

只是一个简短的问题,因为我看到你的科尔多瓦标签是你最好的标签之一。我正确使用'imageSrc'吗?或者我应该修改这个? (如'content:///'或'file:///'或者应该设置为其他内容)(服务器端收到空对象时出现问题) – Ivaro18

0

我面临着同样的问题,当我测试应用程序在离子视图中,但是在我创建apk之后,问题disa有意无意地引发错误。