想在Android中使用PhoneGap的上传PIC到服务器 [英] want to upload a pic to the server using phonegap in android

查看:151
本文介绍了想在Android中使用PhoneGap的上传PIC到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做Android的PhoneGap的项目,我想PIC上传到服务器。

不过,我没有得到的想法,我应该把这个code。

我不能显示任何按钮,上传照片,请大家帮忙。

我是新本。我审阅这个code从PhoneGap的文档。

我想这几个小时,但不能得到更好的解决方案。

这是我的第一个Android的PhoneGap项目。

code:

 < HEAD>
   <脚本类型=文/ JavaScript的字符集=utf-8SRC =科尔多瓦 -  2.4.0.js>< / SCRIPT>
   <脚本类型=文/ JavaScript的字符集=utf-8>
    document.addEventListener(deviceready,onDeviceReady,假);

    传播onDeviceReady(){
        navigator.camera.getPicture(uploadPhoto,
                功能(消息){警报('得到的图像失败); },
              {质量:50,destinationType:navigator.camera.DestinationType.FILE_URI,
                sourceType的:navigator.camera.PictureSourceType.PHOTOLIBRARY}
             );
    }
    功能uploadPhoto(imageURI){
        VAR的选择=新FileUploadOptions();
        options.fileKey =文件;
        options.fileName = imageURI.substr(imageURI.lastIndexOf('/')+ 1);
        options.mimeType =为image / jpeg;

        变种PARAMS = {};
        params.value1 =测试;
        params.value2 =参数;

        options.params = PARAMS;

        VAR英尺=新的文件传输();
        ft.upload(imageURI,EN codeURI(http://some.server.com/upload.php),胜利,失败,期权);
    }

    功能赢(R){
        的console.log(code =+ r.response code);
        的console.log(响应=+ r.response);
        的console.log(已发送=+ r.bytesSent);
    }

    功能失效(错误){
        (发生错误:code =+错误code)警告;
        的console.log(上传误差源+ error.source);
        的console.log(上传错误的目标+ error.target);
    }

    < / SCRIPT>
 < /头>
 <身体GT;
    &其中; H1物实施例&所述; / H1>
    < P>上传文件< / P>
 < /身体GT;
 

解决方案

您使用的下一个code解决您的问题:

 <脚本类型=文/ JavaScript的>
功能uploadFromGallery(){

    //从指定源检索图像文件位置
    navigator.camera.getPicture(uploadPhoto,
                                功能(消息){警报('得到的图像失败); },
                                {质量:50,
                                destinationType:navigator.camera.DestinationType.FILE_URI,
                                sourceType的:navigator.camera.PictureSourceType.PHOTOLIBRARY}
                                );

}

功能uploadPhoto(imageURI){
    VAR的选择=新FileUploadOptions();
    options.fileKey =文件;
    options.fileName = imageURI.substr(imageURI.lastIndexOf('/')+ 1)+'。PNG;
    options.mimeType =text / plain的;

    VAR PARAMS =新的对象();

    options.params = PARAMS;

    VAR英尺=新的文件传输();
    ft.upload(imageURI,EN codeURI(http://some.server.com/upload.php),胜利,失败,期权);
}

功能赢(R){
    的console.log(code =+ r.response code);
    的console.log(响应=+ r.response);
    的console.log(已发送=+ r.bytesSent);
}

功能失效(错误){
    (发生错误:code =+错误code)警告;
    的console.log(上传误差源+ error.source);
    的console.log(上传错误的目标+ error.target);
}
< / SCRIPT>
< /头>
<身体GT;
   <数据角色=按钮的onClick =uploadFromGallery();>上传从画廊< / A>
< /身体GT;
 

查看这篇文章的详细信息: <一href="http://stackoverflow.com/a/13862151/1853864">http://stackoverflow.com/a/13862151/1853864

I am doing project in android phonegap and I want to upload pic to the server.

But I am not getting idea, where should I put this code.

I can't show any buttons to upload photos, please help.

I am new in this. I refereed this code from phonegap documentation.

I am trying this for hours, but can't get the better solution.

It's my first android phonegap project.

Code:

   <head>
   <script type="text/javascript" charset="utf-8" src="cordova-2.4.0.js"></script>
   <script type="text/javascript" charset="utf-8">        
    document.addEventListener("deviceready", onDeviceReady, false);

    function onDeviceReady() {           
        navigator.camera.getPicture(uploadPhoto,
                function(message) { alert('get picture failed'); },
              { quality: 50, destinationType:    navigator.camera.DestinationType.FILE_URI,
                sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY }
             );
    }
    function uploadPhoto(imageURI) {
        var options = new FileUploadOptions();
        options.fileKey="file";
        options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
        options.mimeType="image/jpeg";

        var params = {};
        params.value1 = "test";
        params.value2 = "param";

        options.params = params;

        var ft = new FileTransfer();
        ft.upload(imageURI, encodeURI("http://some.server.com/upload.php"), win, fail, options);
    }

    function win(r) {
        console.log("Code = " + r.responseCode);
        console.log("Response = " + r.response);
        console.log("Sent = " + r.bytesSent);
    }

    function fail(error) {
        alert("An error has occurred: Code = " + error.code);
        console.log("upload error source " + error.source);
        console.log("upload error target " + error.target);
    }

    </script>
 </head>
 <body>
    <h1>Example</h1>
    <p>Upload File</p>
 </body>

解决方案

You solve your problem using the next code:

<script type="text/javascript">  
function uploadFromGallery() {

    // Retrieve image file location from specified source
    navigator.camera.getPicture(uploadPhoto,
                                function(message) { alert('get picture failed'); },
                                { quality: 50, 
                                destinationType: navigator.camera.DestinationType.FILE_URI,
                                sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY }
                                );

}

function uploadPhoto(imageURI) {
    var options = new FileUploadOptions();
    options.fileKey="file";
    options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1)+'.png';
    options.mimeType="text/plain";

    var params = new Object();

    options.params = params;

    var ft = new FileTransfer();
    ft.upload(imageURI, encodeURI("http://some.server.com/upload.php"), win, fail, options);
}

function win(r) {
    console.log("Code = " + r.responseCode);
    console.log("Response = " + r.response);
    console.log("Sent = " + r.bytesSent);
}

function fail(error) {
    alert("An error has occurred: Code = " + error.code);
    console.log("upload error source " + error.source);
    console.log("upload error target " + error.target);
}
</script>
</head>
<body>
   <a data-role="button" onClick="uploadFromGallery();">Upload from Gallery</a> 
</body>

See more info on this post: http://stackoverflow.com/a/13862151/1853864

这篇关于想在Android中使用PhoneGap的上传PIC到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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