Cordova FileTransfer sends file but nothing received

﹥>﹥吖頭↗ 提交于 2019-12-08 02:32:38

问题


I'm sending a video recorded via Cordova's MediaCapture plugin to a remote server via the FileTransfer plugin, but nothing - neither the file, nor any data whatsoever - is arriving at the server end. The server receives the request, but it seems to be empty.

According to Cordova, everything goes fine. Here's the readout from the success callback:

And here's my JS: (mediaFiles[0] is the captured video file)

var options = new FileUploadOptions();
options.fileName = 'foo.bar';
options.mimeType = mediaFiles[0].type;
options.params = {
    mime: mediaFiles[0].type
};

var ft = new FileTransfer();
ft.upload(
    mediaFiles[0].fullPath,
    encodeURI("http://xxxxxx.com/receive-video.php"),
    function (r) {
        console.log(r);
        alert('sent file!');
    },
    function (error) {
        alert('error');
        console.log(error);
    },
    options,
    true
);

(Note the last param, trustAllHosts, is set to true since my test server is self-signed.)

Cordova clearly thinks it's sent data, but my PHP script disagrees. Here's my PHP:

file_put_contents(
    'readout.txt',
    "Payload\n----------\n".
    file_get_contents('php://input').
    "\n\nRequest\n----------\n".
    print_r($_REQUEST, 1).
    "\n\nFiles\n----------\n".
    print_r($_FILES, 1).
    "\n\nPost\n----------\n".
    print_r($_POST, 1)
);

As you can see, I'm looking pretty much everywhere. All these result in empty readouts, however, in readout.txt.

What am I doing wrong?


回答1:


It turned out the chunkedMode param (in options) was the culprit.

In case this helps anyone else, disable this (it's true by default) and all should be fine.

options.chunkedMode = false;

Not sure how to explain the behaviour of the empty request with it turned on, though. The param exists to send the file in chunks, to allow for progress feedback of some sort.



来源:https://stackoverflow.com/questions/45734955/cordova-filetransfer-sends-file-but-nothing-received

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!