使用worklight http适当格式发布图像 [英] post image using worklight http adapter proper format

查看:171
本文介绍了使用worklight http适当格式发布图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用HTTP适配器从Worklight V6应用程序将图像(作为表单的一部分)发布到PHP服务器。图像是base64编码的

I am trying to post an image (as part of a form) to a PHP server from a Worklight V6 app using the HTTP adapter. The image is base64 encoded

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-urlencoded

In my HTTP adapter I uriencode the form data and send it x-www-form-urlencoded

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照片查看器无法打开此图片,因为文件似乎已损坏,损坏或太大。

When I decode the data and save it to a file using my php server, the Windows photo viewer displays an error message " Windows photo viewer can't open this picture because the file appears to be damaged, corrupted or is too large".

我是一个php初学者,但这里是我使用的PHP代码

I am a php beginner, but here is the php code that I used

<?php
$image = $_POST['image']

$decoded=base64_decode($image);

file_put_contents('C:\apache\htdocs\myStuff\newImage.JPG',$decoded);

我确定我犯了某种愚蠢的初学者错误,但我不确定如果它在我的适配器代码,PHP代码或我的worklight客户端代码中。提前感谢您的任何建议。

I'm sure I'm making some sort of silly beginner mistake, but I'm not sure if it's in my adapter code, the php code or in my worklight client code. Thank you in advance for any suggestions.

JT

推荐答案

我认为问题是base64编码数据前面的data:image / jpeg; base64。将图像发送到没有该前缀的适配器,或者在发布到服务之前在适配器中将其剥离。

I think the problem is "data:image/jpeg;base64," at the front of the base64 encoded data. Either send the image to the adapter w/o that prefix, or strip it off in the adapter before posting to the service.

这篇关于使用worklight http适当格式发布图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆