PhoneGap相机/照片到服务器上传,失败,随机对话框出现

时间:2013-08-28 04:39:10

标签: javascript jquery ios cordova

我正在使用phonegap 3.0.0

在尝试将文件上传到服务器的过程中,我遇到了一些意想不到的事情。首先,脚本错误:

  

错误:SyntaxError:意外的令牌':'phonegap.js的第624行

(我认为这是js的旧版本,因为我只能在github上找到一个)

接下来我得到的东西,我不知道为什么/如果我从未在其他应用程序上看到它会如何提示...是一个小小的提示对话框:

enter image description here

当我在对话框上单击“确定”时,它会尝试执行脚本的其余部分,并给出上述错误。

我用来上传的脚本几乎与手机网站上的字母相同。http://docs.phonegap.com/en/3.0.0/cordova_file_file.md.html#File

$('#select_photo').on('click', function()
{
    $('#choice_of_file').click();
});
$('#upload_photo').on('click', function()
{
    if(fmr.nullCheck($('#choice_of_file').val()) == true)
    {
        alert('Please Choose a Photo');
    }
    else
    {
        uploadPhoto($('#choice_of_file').val());
    }
});

// Wait for device API libraries to load
//
(function(){document.addEventListener("deviceready", onDeviceReady, false);})();

// device APIs are available
//
function onDeviceReady() {
    // 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);
    options.mimeType="image/jpeg";

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

    options.params = params;

    var ft = new FileTransfer();
    ft.upload(imageURI, encodeURI(domainURL+'upload/wizard/'+MembId), 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);
}

表单的HTML正在阅读..

<div style="position:absolute;top:-2000px;left:-2000px;background-color:#FFF;z-index:0" id="hide_file_input">
    <form enctype="multipart/form-data" id="upload_profile_image" action="#none" method="post">
        <input type="file" id="choice_of_file" name="choice_of_file">
    </form>
</div>

1 个答案:

答案 0 :(得分:0)

Filekey应该是文件字段的名称属性

options.fileKey="choice_of_file";

如果你可以在执行所有函数之前调用deviceready并将navigator.camera.getPicture放在文件字段的onclick事件中,那么它会更好