开发者

Camera doesn't work on phonegap when files in server?

I wrote a client for my server based on jquery mobile. I just cannot figure out how to transfer photos to my server because when I use GET method to post base64 strings, the server says the URI is too long.

So I tried to move the webpages and scripts to server and left a local file to redirect to server page. But right now, I can take photos but there seems have no callback when I finished.

Here is what I did

var imgTakePhotoStatusBase64="Wyk+HjAxHTAyNzg3MDUdODQwHTAxOR0wMDAwMDAwMDAwMDAwMDAdRkRFQh0wMDAwMDAwHTA0MB0dMS8xHTUwLjVMQh1OHVcgMzR0aCBTdHJlZXQdQXVzdGluHVRYHSAeMDYdMTBaR0QwMDQdMTFaUmVjaXBpZW50IENvbXBhbnkgTmFtZR0xMlo5MDEyNjM3OTA2HTE0WioqVEVTVCBMQUJFTCAtIERPIE5PVCBTSElQKiodMjNaTh0yMlocWR0yMFogHDAdMjZaNjEzMxwdHgQ=";

function TextStatus(){
        var s=window.prompt("","请输入广播的内容");
        if(s!=null&&s!=""){
                SetStaus(s);
        }
}

$("#pageTakePhotoStatus").live("pageshow", function() {
        TakePhotoStatus()
});

function TakePhotoStatus(){
        imageData="";
        navigator.camera.getPicture(onTakePhotoStatusSuccess, onTakePhotoStatusFail, { quality: 75 });
}

function onTakePhotoStatusSuccess(imageData) {
    var image = document.getElementById('imgTakePhotoStatus');
    image.src = "data:image/jpeg;base64," + imageData;
        imgTakePhotoStatusBase64=imageData;
}

function onTakePhotoStatusFail(message) {
    alert('获取照开发者_JS百科片失败,因为: ' + message);
}

function TakePhotoStatusOk(){
        try
        {
                var myDate = new Date();
        var r=myDate.getTime();
                var path="uploads.share/"+r+"/"+r+".jpg";
            doMethod("ImageUploadByBase64String","{'str':'"+e(imgTakePhotoStatusBase64)+"','path':'"+e(path)+"'}",null,
        function(result){
                            doMethod("PublishPhotoes","{'located':'false','path':'"+e(path)+"','text':'"+e("描述:"+$("#txaTakePhotoStatusDes").val())+"'}",null,
                function(r){
                    alert("您的照片已经发布");
                                        history.back();
                });
        });
        }catch(err)
        {
        alert(err.description);
        }
}

But onTakePhotoStatusSuccess doesn't work when I take a photo. When I run it in local, it is just fine.


Camera works with phonegap, so if the user has your code running as an app it's ok. It won't work as a webapp, because there's no phonegap then.

Now the main issue: POST might refuse to work cross-domain (not sure if phonegap can be set to allow it). If you can - use POST. If not, read:

GET can hold as much data as the browser is willing to send and the server is willing to catch. There is no standard. IE sends 2048 characters (2kb), FF sends over 100kb etc. You have to test your server and maybe configure it to accept large GET queries.

Now the fun part - if you know the length of GET that is avaliable to you, you can transfer data in chunks.

  1. get length of data and divide by chunk length
  2. send the result as the first query so that the server knows how many chunks to expect
  3. send N more queries with chunks of data

Why not send and then confirm that it's all done? Well, it's asynchronous, so you never know.

This works. I did some different implementations of sending lots of data cross-domain with JSONP


Flimzy is right. You should use a POST request to send back those huge images. POST requests are not limited by length like GET requests are.

Also, I can't imagine the camera working properly when used from a remotely loaded page. That kind of stuff is typically sandboxed by the platform. I can't say for certain that it wouldn't work, but I would be very surprised if it did.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜