android - Cordova 安卓 : Empty request while uploading file using official cordova-plugin-file-transfer

标签 android cordova cordova-plugins

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

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

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
(
)

一个简单的 post 请求就可以了:

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
(
)

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

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

最佳答案

弄明白了,这不是 cordova 端的问题,我的 LEMP 设置不正确。 Cordova 代码完美运行。

关于android - Cordova 安卓 : Empty request while uploading file using official cordova-plugin-file-transfer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40571459/

相关文章:

android - minHeight 不适用于相对布局

android - 由于列不存在而强制关闭游标

android - 使用地理定位 api cordova/phonegap 查找距离、速度和加速度

android.support.v4.app 不存在

android - Cordova 中的实时数据使用监控

android - Cordova 将错误的参数传递给 native

android - Android Studio重复的zip条目transformClassesAndResourcesWithProguard

带照片的 Android ListView

安卓目标 : not installed in Ubuntu

java - 检测 iOS Cordova 4.0 应用程序中的内存泄漏