【问题标题】:Cannot Upload to SAP Content Server using Cordova File Transfer plugin无法使用 Cordova 文件传输插件上传到 SAP Content Server
【发布时间】:2017-05-16 17:31:31
【问题描述】:

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

Component img1 in Document 0000000018 updated

插件还会以bytesent的成功代码作为响应,这与图像的大小相同,所以我假设图像已上传。

但是当我使用get 函数来检索我的图像时,什么都没有。身体是空的。当我使用网络浏览器时,它会让我下载一个空的ContentServer.dll 文件,而当我使用 Postman 时,响应正文是空的。 AFAIK,在这样的成功案例中,主体就是图像,而对于 Postman,它在响应的主体中给出图像的二进制形式。

对于我的代码,我使用 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。我想我的上传代码对此负责。不知何故,数据在上传时没有正确的图像格式。有什么建议吗?

【问题讨论】:

    标签: cordova cordova-plugins sapui5


    【解决方案1】:

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

    • FileTransferOption 中的chunkedMode 设置为false。 SAP Content Server 不接受分块数据,至少对于 httpMethod = "PUT"AFAIK。
    • 您可能需要设置trustAllHosts = true,具体取决于您的 BASIS 团队安装 SAPCS 的方式。我不确定这部分,但无论如何,SAPCS 主要用于内部使用,所以trustAllHosts = true 只是让事情变得更容易。如果我理解错了问题,请纠正我!
    • 默认情况下,请求的Content-Type 将是multipart/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"
      }
      

      这将适用于我的情况。我还没有测试过网络连接速度慢的性能,所以我将来可能会做一些性能增强。

    【讨论】:

      猜你喜欢
      • 2016-05-14
      • 2014-10-09
      • 2017-01-21
      • 2018-02-17
      • 1970-01-01
      • 2017-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多