使用Phonegap FileTransfer在服务器上上传文件

时间:2014-04-01 07:37:15

标签: cordova

当我使用

上传文件时,我遇到了一个非常奇怪的问题
ft.upload(fileURI,encodeURI(urlToSave) , win, fail, options, true);

它不起作用。我从未收到过服务器的回复。我也试过

xhr.onreadystatechange = function () {
    if (xhr.readyState === 4) {
        if (xhr.status == 200 && xhr.status < 300) {
//          document.getElementById('div1').innerHTML = xhr.responseText;
            alert ('status is ' + xhr.status);
            alert(xhr.responseText);
        }
    }
}

至少我在这里收到回复,File not sended successfully。但是在ft.upload()的情况下,url永远不会在服务器上点击。

但是在使用HttpRequest时,我可以在服务器上记录命中。

当我们使用internal server进行测试时,还有一件事情ft.Upload()工作正常,但是当我们使用live server时,ft.Upload()永远不会成功。

在我config.xml我已将所有网址列入白名单

<access origin="*"/>

知道这里会发生什么吗?

1 个答案:

答案 0 :(得分:0)

试试我的代码

的javascript

var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = imageURI.substr(imageURI.lastIndexOf('/') + 1);
options.mimeType = "image/jpeg";
var params = new Object();
params.productId = data.productId;
params.imageLink = data.productId + "_1";
options.params = params;
options.chunkedMode = false;
var ft = new FileTransfer();
ft.upload(imageURI, "https://example.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);
    window.location.replace("#linkshare");
}

function fail(error) {
    $.mobile.loading('hide');
    navigator.notification.alert("An error has occurred: Code = " + error.code, null, 'Alert', 'OK');
}

PHP

move_uploaded_file($_FILES["file"]["tmp_name"], 'https://example.com/uploads');