I am attempting to record audio on an iPhone using PhoneGap, then send that audio to a server. I am using PhoneGaps Media APIs to make the recording, then the PhoneGap file transfer API to send the file to the server.
I am able to make the recording just fine, and playing it back works perfectly. When I try to send it to the server, however, the recording shows up on the server but it says the file is 0k big.
I've done fairly extensive searching on this issue and found others that have had this problem. For example: https://groups.google.com/forum/#!topic/phonegap/zjzSs6JVokE
function win(r) {
alert("Code = " + r.responseCode);
alert("Response = " + r.response);
alert("Sent = " + r.bytesSent);
}
function fail(error) {
alert("An error has occurred: Code = " + error.code);
console.log("upload error source " + error.source);
console.log("upload error target " + error.target);
}
function upLoad() {
var options = new FileUploadOptions();
options.fileKey="file";
options.fileName=myPath.substr(myPath.lastIndexOf('/')+1);
options.mimeType="audio/wav";
var params = new Object();
var headers={'headerParam':'headerValue'};
options.headers = headers;
options.chunkedMode = false;
var ft = new FileTransfer();
ft.upload(encodeURI(myPath), encodeURI("http://myserver.com/upload.php"), win, fail, options);
}
Here is the code on the server side:
print_r($_FILES);
$new_image_name = "testFile.wav";
move_uploaded_file($_FILES["file"]["tmp_name"], "/var/www/wwwroot/recordings/".$new_image_name);
I think that this may be an issue with the fact that I am sending .wav files. When I send the file over, r.bytesSent usually shows about 200 to 400 bytes (regardless of the size of the file), so it seems like the actual content of the file just isn't being sent over.
I have tested the above code with a simple text file and it goes through just fine, so I don't think it's a permissions or syntactical issue. I haven't tried this with image files, but I can't imagine it makes to much of a difference what I am sending.
Has anyone done this successfully?
I managed to solve this a few days ago. It's about the myPath variable in the question's example. myPath can't be anything other than the tmp folder (the temporary folder phonegap/device creates) So when you try to send a file, you know for sure a copy of that file was made in the tmp folder. So do this just before the ft.upload:
myPath = myPath.replace(myFolderPath, 'tmp');
In my case myFolderPath was 'Documents\media'. The Documents path is where the custom created files are kept on your device.
I hope this saves people from all visited google search pages :)
(This worked for phonegap 3.0.0)
来源:https://stackoverflow.com/questions/17516493/phonegap-wav-upload-from-an-ios-device-is-creating-a-0k-file-on-the-server