I am working with Cordova, and am trying to upload an audio file to my server through a PHP script.
I am working off of this example
https://cordova.apache.org/blog/2017/10/18/from-filetransfer-to-xhr2.html
The example code suggests to pass the read-in file object as a blob.
reader.onloadend = function() {
// Create a blob based on the FileReader "result", which we asked to be retrieved as an ArrayBuffer
var blob = new Blob([new Uint8Array(this.result)], { type: "image/png" });
var oReq = new XMLHttpRequest();
oReq.open("POST", "http://mysweeturl.com/upload_handler", true);
oReq.onload = function (oEvent) {
// all done!
};
// Pass the blob in to XHR's send method
oReq.send(blob);
};
I have modified the code to attach it as a data object as the file isn't being created
window.resolveLocalFileSystemURL(fileSystem, function (dir) {
dir.getFile(fileName, {create: true}, function (fileEntry) {
fileEntry.file(function (file) {
var reader = new FileReader();
reader.onloadend = function() {
var data = new FormData();
var oReq = new XMLHttpRequest();
oReq.open("POST", "http://mywebsite.com/up.php", true);
var blob = new Blob([new Uint8Array(this.result)], { type: "audio/mpeg" });
data.append('fileToUpload', blob );
oReq.onload = function (oEvent) {
// all done!
};
// Pass the blob in to XHR's send method
oReq.send(data);
};
// Read the file as an ArrayBuffer
reader.readAsArrayBuffer(file);
}
...
My php
$target_dir = $_SERVER['DOCUMENT_ROOT'] . "/uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
//echo "uploaded";
} else {
//echo "error";
}
- I have confirmed I have read/write privileges.
- I have confirmed I can upload the test audio file using a standard html form and the above php script.
- I have checked my vhost error logs and Apache system logs and have not seen any errors. I can see the POST being made but no error messages.
- I have confirmed the file object obtained by Cordova is accurate and does exist.
- I have tried several different mime types with no effect
Any help would be much appreciated.
Thanks