cordova上的文件上传失败

时间:2016-08-04 10:01:44

标签: cordova phonegap-plugins cordova-plugins

使用cordova文件传输插件我尝试上传图像。 但上传失败了。 我的代码如下所示。

let answers = {}

$('input[type=radio]').on('click', function (event) {
  let question = event.currentTarget.name
  let answer = event.currentTarget.value
  answers[question] = answer
  document.getElementById('result').innerHTML = JSON.stringify(answers, null, 2)
})

提前致谢。

1 个答案:

答案 0 :(得分:0)

好的,那么你的代码应该如下:

<强> JavaScript的:

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

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
    pictureSource = navigator.camera.PictureSourceType;
    destinationType = navigator.camera.DestinationType;
}

function clearCache() {
    navigator.camera.cleanup();
}

var retries = 0;
function onCapturePhoto(fileURI) {
    var win = function (r) {
        clearCache();
        retries = 0;
        alert('Done!');
    }

    var fail = function (error) {
        if (retries == 0) {
            retries ++
            setTimeout(function() {
                onCapturePhoto(fileURI)
            }, 1000)
        } else {
            retries = 0;
            clearCache();
            alert('Ups. Something wrong happens!');
        }
    }

    var options = new FileUploadOptions();
    options.fileKey = "file";
    options.fileName = fileURI.substr(fileURI.lastIndexOf('/') + 1);
    options.mimeType = "image/jpeg";
    options.params = {}; // if we need to send parameters to the server request
    var ft = new FileTransfer();
    ft.upload(fileURI, encodeURI("http://host/upload"), win, fail, options);
}

function capturePhoto() {
    navigator.camera.getPicture(onCapturePhoto, onFail, {
        quality: 100,
        destinationType: destinationType.FILE_URI
    });
}

function onFail(message) {
    alert('Failed because: ' + message);
}

<强> PHP

在我的服务器端制作php文件我正在使用xampp php所以我将我的php文件放在c:/xampp/htdocs并从xampp cotrol pannel启动apache然后我将此行设为encodeURI("192.168.1.124/uploadfile.php")

<强> uploadfile.php

<?php
move_uploaded_file($_FILES["file"]["tmp_name"], '/path/to/file');
?>

如果您有任何疑惑,请发表评论。