【问题标题】:how to pass encoded base64 data to HTTP worklight adapter如何将编码的 base64 数据传递给 HTTP 工作灯适配器
【发布时间】:2015-07-21 04:52:28
【问题描述】:

我已使用 cordova(navigator.camera.getPicture) 从设备捕获图像。我使用文件阅读器将 fileURI 转换为 base64。但是,当我将 base64 url​​ 分配为 img src 而如果我将相同的字符串传递给 HTTP 适配器(Worklight)时,我看到编码数据被截断。请帮忙。

提前致谢。

源代码:

    function tryToSend(evt) {
        encoding = evt.target.result;
        console.log("Encoded File: "+encoding);
        Ext.ComponentQuery.query('#encodedImage')[0].setHtml('<img style="height: 100px; width: 100px;" src="'+encoding+'" />');
        Ext.ComponentQuery.query('#encodedImage')[0].setHidden(false);
    }
    function win(file) {
        alert("FileName:"+file.name + ' & Type:' + file.type);
        selectedFileName = file.name;
        Ext.ComponentQuery.query('#originalImage')[0].setHtml('<img style="height: 100px; width: 100px;" src="'+file.fullPath+'" />');
        Ext.ComponentQuery.query('#originalImage')[0].setHidden(false);
        var reader = new FileReader();
        reader.onloadend = tryToSend;
        var encoded = reader.readAsDataURL(file); 
    }
    function fail(error) {
        console.log(error);
    }
    function onResolveSuccessCompleted(fileEntry) {
        fileEntry.file(win, fail);
    }

    function onResolveFailed(error) {
        console.log(error);
    }
//Call on click of take pic button
function capPic(){
 navigator.camera.getPicture(onCapturePhoto, onFail, {
        quality: 50,
        destinationType: destinationType.FILE_URI,
        sourceType: Camera.PictureSourceType.CAMERA,
        mediaType: navigator.camera.MediaType.ALLMEDIA,
    });
}
//Success
function onCapturePhoto(fileURI) {
    window.resolveLocalFileSystemURI(fileURI, onResolveSuccessCompleted, onResolveFailed);
    fileDetails.push({
        base64ImageData:encoding,
        fileName: selectedFileName,
    });
   alert("File Selected. Please Upload Now");
}

//Sending fileDetails array to HTTP adapter as parameter
var invocationData = {
            adapter : 'SAMPLE_ADAPTER',
            procedure : 'uploadFileNow',
            parameters : [fileDetails]
    };  
    WL.Client.invokeProcedure(invocationData, {
        onSuccess : fileUploadOK,
        onFailure : fileUploadFail,
    });

1) 在 Logcat 中,tryToSend Fn 中的编码完全打印,而下一行 console.log 给出截断的代码

//Ajax 调用 Ext.Ajax.request({ url: url, method:'POST', params:fileDetails, success: function(response){ console.log(response); }, failure:function(response){ console.log(response); } });

【问题讨论】:

  • 为什么要将base64字符串作为img src的值?为什么你会提到适配器?以及如何提供一些代码或更好地解释您的问题?
  • 我只是想检查它是否编码正确,因此将该编码代码分配给 img src 并且很好,但是如果我将相同的代码传递给 HTTP 适配器,它会被截断
  • 如何转换并发送?将代码添加到您的图像中。是 GET 请求吗?
  • 已添加代码。请检查
  • 此代码未显示您如何发送它。请再看看我的问题。

标签: image cordova base64 ibm-mobilefirst truncated


【解决方案1】:

在我的 logcat 中,console.log 一次只能打印大约 4k 个字符。所以尝试比较编码后的 url 的长度来检查它是否真的被截断了。

【讨论】:

    猜你喜欢
    • 2019-08-30
    • 1970-01-01
    • 1970-01-01
    • 2020-07-28
    • 1970-01-01
    • 1970-01-01
    • 2018-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多