Image 如何在android上使用cordova/phonegap将图像和文本上传到远程服务器

Image 如何在android上使用cordova/phonegap将图像和文本上传到远程服务器,image,cordova,upload,Image,Cordova,Upload,请我在一个项目上工作,需要从手机摄像头获取照片,填写两个文本框,并使用cordova/phonegap将它们上传到远程服务器。我已经试了好几个星期了,运气不好。我正在android平台上构建。提前感谢。有一个文件传输插件(您可能会尝试使用,也可能不会尝试使用;您没有提供详细信息)用于这些事情。或者可以直接使用javascript,完全忽略cordova/phonegap。详细信息将在很大程度上取决于服务预期的交互方式。有一个文件传输插件(您可能会尝试使用它,也可能不会尝试使用它;您没有提供详细信

请我在一个项目上工作,需要从手机摄像头获取照片,填写两个文本框,并使用cordova/phonegap将它们上传到远程服务器。我已经试了好几个星期了,运气不好。我正在android平台上构建。提前感谢。

有一个文件传输插件(您可能会尝试使用,也可能不会尝试使用;您没有提供详细信息)用于这些事情。或者可以直接使用javascript,完全忽略cordova/phonegap。详细信息将在很大程度上取决于服务预期的交互方式。

有一个文件传输插件(您可能会尝试使用它,也可能不会尝试使用它;您没有提供详细信息)。或者可以直接使用javascript,完全忽略cordova/phonegap。详细信息将在很大程度上取决于服务预期的交互方式。

有一个文件传输插件(您可能会尝试使用它,也可能不会尝试使用它;您没有提供详细信息)。或者可以直接使用javascript,完全忽略cordova/phonegap。详细信息将在很大程度上取决于服务预期的交互方式。

有一个文件传输插件(您可能会尝试使用它,也可能不会尝试使用它;您没有提供详细信息)。或者可以直接使用javascript,完全忽略cordova/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";
// this will get value of text field
        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(“上传错误源”+错误源);
日志(“上传错误目标”+错误目标);
}
选择照片:
名字:
姓氏:
工作地点:

这个代码对我有用。希望这有帮助

创建两个可以分别调用的函数。一个函数用于获取图像,另一个函数用于上载图像。 你可以像下面这样做

<!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";
// this will get value of text field
        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=“文件”