【问题标题】:Cordova Android: Empty request while uploading file using official cordova-plugin-file-transferCordova Android:使用官方 cordova-plugin-file-transfer 上传文件时出现空请求
【发布时间】:2016-11-29 12:44:57
【问题描述】:

尝试使用 Apache 在https://github.com/apache/cordova-plugin-file-transfer 提供的官方cordova-plugin-file-transfer 将文件上传到服务器。

创建一个空的cordova项目,设置文件选择器(https://github.com/don/cordova-filechooser)和文件上传器,并运行以下代码:

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

    var fail = function (error) {
        alert("An error has occurred: Code = " + error.code);
        console.log("upload error source " + error.source);
        console.log("upload error target " + error.target);
    }

    var options = new FileUploadOptions();
    options.fileKey = "upfile";
    options.fileName = "test.jpg";
    options.mimeType = "image/jpeg";
    options.httpMethod = "POST";

    var params = {};
    params.value1 = "test";
    params.value2 = "param";

    options.params = params;

    var ft = new FileTransfer();
    ft.upload(fileURL, encodeURI("http://example.com/test.php"), win, fail, options);
}

function getFile() {
    fileChooser.open(function(uri){
        //alert(uri);
        //document.getElementById('img1').setAttribute('src', uri);
        console.log(uri);
        servUpload(uri);
    }, function(err){
        console.log(err);
    });
}
getFile();

(注意我设置的帖子参数)。

我的test.php 包含以下内容(只是回显所有文件,发布并获取变量)。

<?php
print_r($_FILES);
print_r($_POST);
print_r($_GET);
?>

代码运行良好,我可以选择一个文件,尝试上传似乎需要一些时间。但是没有任何错误,服务器会发现它没有从客户端收到任何信息(没有文件,也没有我在代码中设置的 POST 参数):

Response = Array
(
)
Array
(
)
Array
(
)

一个简单的发布请求虽然有效:

var http = new XMLHttpRequest();
var url = "http://example.com/test.php";
var params = "lorem=ipsum&name=binny";
http.open("POST", url, true);

//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

http.onreadystatechange = function() {//Call a function when the state changes.
    if(http.readyState == 4 && http.status == 200) {
        console.log(http.responseText);
    }
}
http.send(params);

这会返回:

Array
(
)
Array
(
    [lorem] => ipsum
    [name] => binny
)
Array
(
)

我不知所措,我已经确保文件选择器确实有效(我一直在使用图像文件进行测试并测试我可以使用图像设置 &lt;img&gt; 元素作为它的来源)。

有什么想法吗?提前致谢。

【问题讨论】:

    标签: android cordova cordova-plugins


    【解决方案1】:

    想通了,对科尔多瓦来说不是问题,我的 LEMP 设置不正确。 Cordova 代码完美运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-09
      • 2017-08-10
      • 1970-01-01
      • 2017-06-12
      • 1970-01-01
      • 2020-11-25
      • 1970-01-01
      相关资源
      最近更新 更多