To upload recorded audio or video files to a PHP server, capture the recording as a Blob and use FormData to POST it via an XMLHttpRequest. The server expects two pieces of data for each type (e.g., 'video' or 'audio'):
- A filename string sent with the key
{type}-filename (e.g., video-filename). - The actual Blob file sent with the key
{type}-blob (e.g., video-blob).
This approach works in Chrome, Firefox, Opera, Microsoft Edge, and on Android devices.
var fileType = 'video'; // or "audio"
var fileName = 'ABCDEF.webm'; // or "wav"
var formData = new FormData();
formData.append(fileType + '-filename', fileName);
formData.append(fileType + '-blob', blob);
xhr('save.php', formData, function (fName) {
window.open(location.href + fName);
});
function xhr(url, data, callback) {
var request = new XMLHttpRequest();
request.onreadystatechange = function () {
if (request.readyState == 4 && request.status == 200) {
callback(location.href + request.responseText);
}
};
request.open('POST', url);
request.send(data);
}