Cordova Android: Empty request while uploading file using official cordova-plugin-file-transfer -
trying upload file server using official cordova-plugin-file-transfer
provided apache @ https://github.com/apache/cordova-plugin-file-transfer.
created empty cordova project, setup file picker (https://github.com/don/cordova-filechooser) , file uploader, , ran following code:
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();
(note post params set).
my test.php
contains following (just echos of file, post , vars).
<?php print_r($_files); print_r($_post); print_r($_get); ?>
the code runs fine, can pick file , seems take bit attempt upload. without error server picks has not received info client (no files, nor post params set in code):
response = array ( ) array ( ) array ( )
a simple post request works though:
var http = new xmlhttprequest(); var url = "http://example.com/test.php"; var params = "lorem=ipsum&name=binny"; http.open("post", url, true); //send proper header information along request http.setrequestheader("content-type", "application/x-www-form-urlencoded"); http.onreadystatechange = function() {//call function when state changes. if(http.readystate == 4 && http.status == 200) { console.log(http.responsetext); } } http.send(params);
this returns:
array ( ) array ( [lorem] => ipsum [name] => binny ) array ( )
i'm @ loss can do, i've made sure file picker works (i've been testing image file , tested can set <img>
element image source).
any ideas? in advance.
figured out, wasn't problem on cordova's end, lemp wasn't setup right. cordova code works perfectly.
Comments
Post a Comment