我正在尝试使用 HTTP 适配器从 Worklight V6 应用程序将图像(作为表单的一部分)发布到 PHP 服务器。 图像采用base64编码
navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 8,
destinationType: navigator.camera.DestinationType.DATA_URL });
.. 稍后在代码中
$('#myImageImg').attr('src', "data:image/jpeg;base64," + imageData);
我将图像发送到适配器
var img = $('#myImageImg').attr('src');
var formData = {"someField" : name,
"image" : img };
var invocationData = {
adapter : 'emailAdapter',
procedure : 'sendEmail',
parameters : [ formData ]
};
var options = {
onSuccess : sendEmailOK,
onFailure : sendEmailFail,
invocationContext : {}
};
$.mobile.showPageLoadingMsg();
WL.Client.invokeProcedure(invocationData,options);
在我的HTTP适配器中,我对表单数据进行尿液编码并将其发送x-www-form-urlencode。
function sendEmail(inputData) {
var uri = 'myStuff/sendEmail.php';
var imageData="image='" + inputData.image+"'";
var formData = encodeURI(imageData);
var input = {
method : 'post',
returnedContentType : 'html',
path : path,
body: { "contentType" : "application/x-www-form-urlencoded",
'content' : formData
}
当我解码数据并使用我的php服务器将其保存到文件中时,Windows照片查看器显示错误消息" Windows照片查看器无法打开此图片,因为文件似乎已损坏,损坏或太大"。
我是 php 初学者,但这是我使用的 php 代码
<?php
$image = $_POST['image']
$decoded=base64_decode($image);
file_put_contents('C:'apache'htdocs'myStuff'newImage.JPG',$decoded);
我确定我犯了某种愚蠢的初学者错误,但我不确定它是在我的适配器代码、php 代码还是在我的 worklight 客户端代码中。 提前感谢您的任何建议。
JT
我认为问题是"data:image/jpeg;base64",位于base64编码数据的前面。 将映像发送到不带该前缀的适配器,或者在发布到服务之前在适配器中将其剥离。
大卫D救了我。 在适配器代码中,我有
var formData = encodeURI(imageData);
为了使代码正常工作,我们将其更改为
var imageData = "image='" + encodeURIComponent(inputData.image)+"'";
变量输入 = { 方法 : "发布", 返回的内容类型 : 'html', 路径 : 路径,
body: { "contentType" : "application/x-www-form-urlencoded",
'content' : imageData
}
这里的想法是,base64 编码的字符串可以有 1 或 2 个尾随 = 来填充到偶数。 encodeURI 不编码 = 的,所以这些在从适配器到我的 PHP 服务器的转换中丢失了。
同样正如Dave上面所写的,我需要去掉base64编码数据前面的"data:image/jpeg;base64"。 谢谢大卫!!