Cordova 使用phonegap上传图像

Cordova 使用phonegap上传图像,cordova,sencha-touch,sencha-touch-2,Cordova,Sencha Touch,Sencha Touch 2,我是phonegap的新手,在将图像上传到服务器时遇到了很多麻烦 我通过浏览和使用相机将一张图像上传到服务器。 我想用相机上传多张图片,同时浏览画廊 我该怎么做呢?检查一下这个答案,也许它能帮你 <!DOCTYPE html> <html> <head> <title>Submit form</title> <script type="text/javascript" charset="utf-8" src=

我是phonegap的新手,在将图像上传到服务器时遇到了很多麻烦

我通过浏览和使用相机将一张图像上传到服务器。 我想用相机上传多张图片,同时浏览画廊

我该怎么做呢?

检查一下这个答案,也许它能帮你

<!DOCTYPE html>
<html>
  <head>
    <title>Submit form</title>

    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
    <script type="text/javascript" charset="utf-8">

    var pictureSource;   // picture source
    var destinationType; // sets the format of returned value

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

    // device APIs are available
    //
    function onDeviceReady() {
        pictureSource = navigator.camera.PictureSourceType;
        destinationType = navigator.camera.DestinationType;
    }


    // Called when a photo is successfully retrieved
    //
    function onPhotoURISuccess(imageURI) {

        // Show the selected image
        var smallImage = document.getElementById('smallImage');
        smallImage.style.display = 'block';
        smallImage.src = imageURI;
    }


    // A button will call this function
    //
    function getPhoto(source) {
      // Retrieve image file location from specified source
      navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50,
        destinationType: destinationType.FILE_URI,
        sourceType: source });
    }

    function uploadPhoto() {

        //selected photo URI is in the src attribute (we set this on getPhoto)
        var imageURI = document.getElementById('smallImage').getAttribute("src");
        if (!imageURI) {
            alert('Please select an image first.');
            return;
        }

        //set upload options
        var options = new FileUploadOptions();
        options.fileKey = "file";
        options.fileName = imageURI.substr(imageURI.lastIndexOf('/')+1);
        options.mimeType = "image/jpeg";

        options.params = {
            firstname: document.getElementById("firstname").value,
            lastname: document.getElementById("lastname").value,
            workplace: document.getElementById("workplace").value
        }

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

    // Called if something bad happens.
    //
    function onFail(message) {
      console.log('Failed because: ' + message);
    }

    function win(r) {
        console.log("Code = " + r.responseCode);
        console.log("Response = " + r.response);
        //alert("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>
    <form id="regform">
        <button onclick="getPhoto(pictureSource.PHOTOLIBRARY);">Select Photo:</button><br>
        <img style="display:none;width:60px;height:60px;" id="smallImage" src="" />

        First Name: <input type="text" id="firstname" name="firstname"><br>
        Last Name: <input type="text" id="lastname" name="lastname"><br>
        Work Place: <input type="text" id="workplace" name="workPlace"><br>
        <input type="button" id="btnSubmit" value="Submit" onclick="uploadPhoto();">
    </form>
  </body>
</html>

提交表格
var pictureSource;//图像源
var destinationType;//设置返回值的格式
//等待加载设备API库
//
文件。添加的监听器(“deviceready”,OnDeviceraddy,false);
//设备API可用
//
函数ondevicerady(){
pictureSource=navigator.camera.PictureSourceType;
destinationType=navigator.camera.destinationType;
}
//成功检索照片时调用
//
函数onPhotoURISuccess(imageURI){
//显示所选图像
var smallImage=document.getElementById('smallImage');
smallImage.style.display='block';
smallImage.src=imageURI;
}
//一个按钮将调用此函数
//
函数getPhoto(源代码){
//从指定源检索图像文件位置
navigator.camera.getPicture(onPhotoURISuccess,onFail,{质量:50,
destinationType:destinationType.FILE\u URI,
sourceType:source});
}
函数uploadPhoto(){
//所选照片URI位于src属性中(我们在getPhoto上设置了该属性)
var imageURI=document.getElementById('smallImage').getAttribute(“src”);
如果(!imageURI){
警报('请先选择图像');
返回;
}
//设置上传选项
var options=new FileUploadOptions();
options.fileKey=“文件”;
options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
options.mimeType=“image/jpeg”;
options.params={
firstname:document.getElementById(“firstname”).value,
lastname:document.getElementById(“lastname”).value,
workplace:document.getElementById(“workplace”).value
}
var ft=新文件传输();
ft.upload(图像URI、编码URI(“http://some.server.com/upload.php)、赢、输、期权);
}
//如果有什么不好的事情发生了就打电话。
//
函数onFail(消息){
console.log('失败原因:'+消息);
}
函数win(r){
console.log(“Code=“+r.responseCode”);
console.log(“Response=“+r.Response”);
//警报(“响应=”+r.Response);
console.log(“Sent=“+r.bytesent”);
}
功能失败(错误){
警报(“发生错误:Code=“+error.Code”);
console.log(“上传错误源”+错误源);
日志(“上传错误目标”+错误目标);
}
选择照片:
名字:
姓氏:
工作地点:
如果您想从库中获取图像,而不是使用相机,请使用回答getPhoto(pictureSource.PHOTOLIBRARY)中描述的函数; 然而,对于选择各种图像,您将面临一些麻烦,正如您在这里看到的

你必须为此编写一个自定义插件,或者查找是否有人已经制作了一个插件