gpt4 book ai didi

android - 无法使用 Cordova FileTransfer 上传文件

转载 作者:行者123 更新时间:2023-11-30 00:51:33 25 4
gpt4 key购买 nike

我正在尝试使用 FileTransfer Cordova 插件 ( https://github.com/apache/cordova-plugin-file-transfer ) 的上传方法上传文件。最后我几乎复制了项目页面上提到的示例,但仍然没有成功 - 该方法触发成功回调函数,但目标脚本没有接收到文件或任何参数。我的代码是:

调用函数:

var fleNme = TOP_APP_DIR + "/config/config.json";

fileAPI.readFile(fleNme, function (data) {
console.log("File content: " + data); // just to meake sure the file exists and it contains some data

fileAPI.uploadFile("http://posttestserver.com/post.php", fleNme, { par1: "test", par2: "test2" }, null, function (r) {
console.log("Successful upload...");
console.log(JSON.stringify(r));
});
});

上传功能:

fileAPI.uploadFile = function (url, filePath, params, progressFunc, successCallback, failCallback, FStype) {
if (typeof failCallback === "undefined" || failCallback === null)
failCallback = fileAPI.defaultErrorCallback;

// getFileEntry method is used for reading the file before call the upload method, so it's OK
fileAPI.getFileEntry(filePath, function (fileEntry) {
var fileURL = fileEntry.toURL();

var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = fileURL.substr(fileURL.lastIndexOf('/') + 1);
options.mimeType = "text/plain";
options.httpMethod = "POST";

if(typeof params === "undefined" || params === null)
params = {};

options.params = params;

var ft = new FileTransfer();

ft.upload(fileURL, encodeURI(url), successCallback, failCallback, options);
}, failCallback,FStype,false);
};

我控制台的输出是:

来自 posttestserver.com 的日志是:

Time: Sat, 03 Dec 16 11:41:06 -0800
Source ip: 31.30.45.209

Headers (Some may be inserted by server)
REQUEST_URI = /post.php
QUERY_STRING =
REQUEST_METHOD = POST
GATEWAY_INTERFACE = CGI/1.1
REMOTE_PORT = 48730
REMOTE_ADDR = 31.30.45.209
HTTP_ACCEPT_ENCODING = gzip
HTTP_CONNECTION = close
HTTP_HOST = posttestserver.com
HTTP_USER_AGENT = Dalvik/1.6.0 (Linux; U; Android 4.4.2; SM-T320 Build/KOT49H)
CONTENT_TYPE = multipart/form-data; boundary=+++++
UNIQUE_ID = WEMf0kBaMGUAAFmEWtgAAAAK
REQUEST_TIME_FLOAT = 1480794066.2475
REQUEST_TIME = 1480794066

No Post Params.
Empty post body.

== Multipart File upload. ==
Received 0 file(s)

我的目标是 Cordova CLI 6.1.1。我花了一整天尝试不同的方法,但总是得到相同的结果。我在 3 种不同的 Android 设备(HTC 10、Samsung Galaxy Tab SM-T320 和一些旧的 Prestigio 手机)上调试了代码,结果相同。知道出了什么问题吗?

最佳答案

我发现 chunkedMode 的默认设置(true)存在很多问题。切换到 options.chunckedMode = false 应该有助于解决问题。

关于android - 无法使用 Cordova FileTransfer 上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40951893/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com