【发布时间】:2021-08-06 09:48:37
【问题描述】:
我正在尝试在移动设备的浏览器上录制视频并将该视频发送到我的 PHP 服务器。但是当我在 PHP 中检查/调试我的代码时,数组 $_FILES 是空的。由于我缺乏知识,我确信我的 JavaScript 代码有问题。
这是我的 HTML / Javascript 代码:
<body>
<header>
<h1>HTML5 Video & Audio Input</h1>
<h2>Capturing Media with HTML and passing the data to PHP</h2>
</header>
<form method="post" action="serverTest.php" id="myform" enctype="multipart/form-data">
<label for="capture">Capture Media</label>
<input name= "uploadedVideo" type="file" id="videograbado" accept="video/*" capture="user-scalable" multiple />
<video id="player" controls></video>
</form>
<script>
document.addEventListener('DOMContentLoaded', (ev) => {
let form = document.getElementById('myform');
//get the captured media file
let input = document.getElementById('videograbado');
input.addEventListener('change', (ev) => {
console.dir(input.files[0]);
if (input.files[0].type.indexOf("video/") > -1) {
let video = document.getElementById('video');
var video1 = input.files[0];
video = window.URL.createObjectURL(input.files[0]);
var formData = new FormData();
formData.append('video-filename', input.files[0].name);
formData.append('video-blob', video);
xhr('serverTest.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, true);
request.send(formData);
}
}
})
})
</script>
</body>
欢迎任何意见或建议。
来源: https://www.youtube.com/watch?v=dbrez37HlJM
https://github.com/muaz-khan/RecordRTC/tree/master/RecordRTC-to-PHP
【问题讨论】:
标签: javascript php html forms