无法使用Cordova文件传输插件

时间:2017-05-16 17:31:32

标签: cordova cordova-plugins sapui5

我尝试使用Cordova FileTransfer插件从我的SAPUI5应用程序上传图像到SAP Http Content Server。 到目前为止,当我上传时,它会返回成功代码(200)和下面的消息:

Component img1 in Document 0000000018 updated

插件还使用bytesent的成功代码给出响应,这与图片的大小相同,所以我假设图片已上传。

但是当我使用get函数来检索我的图像时,那里什么也没有。尸体是空的。当我使用Web浏览器时,它会让我下载一个空的ContentServer.dll文件,当我使用Postman时,响应正文为空。 AFAIK,在这样的成功案例中,身体就是形象,而对于邮递员来说,它在反应的身体中提供了二进制形象。

对于我的代码,我使用了Cordova Camera(我试过Media Capture但问题相同)插件来拍摄照片如下:

takePhoto: function() {
navigator.camera.getPicture(onCapturePhoto, onFail, {
            quality: 100,
            destinationType: destinationType.FILE_URI
        });
}

成功拍摄照片后,会调用onCapturePhoto功能:

        function onCapturePhoto(fileURI) {

            var options = new FileUploadOptions();
            options.fileKey = "file";
            options.httpMethod = "PUT";
       ** This line below solved my first problem but still cannot solve the 
              original problem as my Update1 mentioned**
            options.chunkedMode = false;
            options.fileName = fileURI.substr(fileURI.lastIndexOf('/') + 1);
            options.mimeType = "image/jpeg";
            options.params = {}; // if we need to send parameters to the server request

            var win = function(r) {
                console.log(r);
                console.log(fileURI);
            };

            var fail = function(error) {
                console.log(error);
                alert('Ups. Something wrong happens!');
            };
            var ft = new FileTransfer();
            var url = "http://serverIP:1090/ContentServer/ContentServer.dll?update&pVersion=0046&contRep=Z1&docId=0000000018&compId=img1";
            ft.upload(fileURI, url, win, fail, options);
}

我猜想数据不会传输到服务器,这就是为什么主体总是空的。但我非常确定语法是否正确,至少不会给出任何错误。因此,我完全糊涂了。有什么建议吗?

Update1:​​到目前为止,我发现了问题:它是options.chunkedMode,它提供了空响应体。现在我可以看到我的回复正文。但问题是我的身体没有呈现为图像而是ContentServer.dll。我想我的上传代码对此负责。不知何故,数据在上传时没有正确的图像格式。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

所以我终于找到了答案。通过添加一些客户标题(在我的情况下,我将添加Content-Type),我已禁用了多部分/表单数据,导致响应正文中出现Content-Disposition,导致对于响应受到损害而无法打开的问题。现在一切似乎都很好。因此,如果有人想使用Cordova FileTransfer插件上传到SAP Content Server,我会总结一些观点:

  • chunkedMode中的FileTransferOption设为false。 SAP Content Server不接受分块数据,至少使用httpMethod = "PUT" AFAIK。
  • 您可能需要设置trustAllHosts = true,具体取决于BASIS团队如何安装SAPCS。我对此部分不太确定,但无论如何,SAPCS主要用于内部使用,因此trustAllHosts = true只会让事情变得更容易。如果我理解错误的话,请修理我!
  • 默认情况下,请求的Content-Typemultipart/form-data,这会导致SAPCS将一些标头数据存储到响应正文中。这将导致存储的数据变得不一致和受损的问题。要解决此问题并能够正确存储数据,您应该添加

    headers = {
     "Content-Type": "image/jpg" //This is equivalent to mimeType so you can put that option here, in my case "image/jpg"
    }
    

    这将适用于我的情况。我还没有在慢速网络连接方面测试性能,所以我可能会在将来做一些性能提升。

相关问题