【问题标题】:PhoneGap File Transfer Image submit formPhoneGap 文件传输图像提交表单
【发布时间】:2012-05-27 23:55:24
【问题描述】:

我不知道如何让 Filetransfer APi 作为提交表单的一部分工作。目前,您选择一张图片并将其自动发送到服务器,但是对于应用程序,我需要与它一起发送其他数据。我需要其他部分正常工作,这只是基于文本/选项,但我被图像难住了。

atm 我只是在看 zac vineyards 代码示例

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4    /strict.dtd">
<html>
<head>
<title>File Transfer Example</title>
<script type="text/javascript" charset="utf-8" src="phonegap-1.2.0.js"></script>
<script type="text/javascript" charset="utf-8">

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

    // PhoneGap is ready
    function onDeviceReady() {
// Do cool things here...
    }

    function getImage() {
        // 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 = new Object();
        params.value1 = "test";
        params.value2 = "param";

        options.params = params;
        options.chunkedMode = false;

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

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

    function fail(error) {
        alert("An error has occurred: Code = " = error.code);
    }

    </script>
</head>
<body>
<button onclick="getImage();">Upload a Photo</button>
</body>
</html>

如果有人有任何想法,将不胜感激。我尝试了很多不同的东西。我已经让它在桌面平台上工作,但是当我使用我的手机时,如果这与它有任何关系的话。它不起作用。谢谢

【问题讨论】:

  • 您已经通过选项对象发送了额外的数据。查看 options.params 对象。

标签: android image forms cordova file-transfer


【解决方案1】:

您添加要发送的键/值对作为“params”对象的成员。假设你想发送 uid=1234 和 server=na ,那么你会这样做:

var params = new Object();
params.uid = "1234";
params.server = "na";

【讨论】:

    【解决方案2】:

    您可以像这样发送其他数据,比如 objetId 和图像:

           options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1)+".jpg";       
           var upload_url="http:YourDomain.com 
           /upload?token="+objectId+"&filenaam="+options.fileName;
            ft.upload(imageURI, upload_url, win, fail, options);
    

    【讨论】:

      猜你喜欢
      • 2014-04-28
      • 2023-03-27
      • 2013-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-11
      • 1970-01-01
      • 2011-12-17
      相关资源
      最近更新 更多