I will post only the part of the Javascript submission, as you will receive in PHP and will process the Stream, this I will leave to your discretion.
For the example below, I will use a input:file
, on it you must perform the upload of a video which you want, the important thing here is how we will do to upload a Blob
(its variable stream
) with a file for the server.
In the example below, I call the Blob
of arquivo
.
//caso não tenha um arquivo de video, baixe um exemplo em:
//http://techslides.com/demos/sample-videos/small.webm
var file = document.getElementById("file");
var enviar = document.getElementById("enviar");
var exibir = document.getElementById("exibir");
var video = document.getElementById("video");
var pegarArquivo = function () {
if (file.files.length > 0) {
var arquivo = file.files[0];
if (arquivo.type.indexOf("video") != -1) {
return arquivo;
} else {
alert("Selecione um arquivo de video");
}
} else {
alert("Selecione um arquivo");
}
return null;
}
exibir.addEventListener("click", function (event) {
var arquivo = pegarArquivo();
if (arquivo) {
var url = URL.createObjectURL(arquivo);
video.src = url;
}
});
enviar.addEventListener("click", function (event) {
var arquivo = pegarArquivo();
if (arquivo) {
var formData = new FormData();
formData.append("arquivo", arquivo);
var httpRequest = new XMLHttpRequest();
httpRequest.open("POST", "%URL com extensão PHP que espera o arquivo%", true);
httpRequest.onreadystatechange = function (event) {
if (httpRequest.readyState == 4) {
if (httpRequest.status == 200) {
alert("arquivo rececido com sucesso");
} else {
alert("ocorreu um erro durante o envio");
}
}
}
httpRequest.send(formData);
}
});
<input id="file" type="file" />
<br />
<input id="exibir" type="button" value="Exibir Video" />
<input id="enviar" type="button" value="Enviar Video" />
<br />
<video id="video" width="320" height="240" loop="" autoplay="" ></video>
When using a FormData
you are sending a form
to the server, you can mount one dynamically as in the example or use an existing form, for example:
var form = document.getElementById("form");
var data = new FormData(form);
...
httpRequest.send(data);
So what we’re basically doing is sending the file inside a form
through a POST request.
But a detail, to send a FormData
, you need to set the header
Content-Type
for multipart/form-data
, this is the default value if using XMLHttpRequest
to carry out the requisition AJAX
.
If using jQuery
, it modifies this property, then you will have to inform it to use the conventional form and not make modifications:
var data = new FormData();
data.append("arquivo", arquivo)
$.ajax({
url: "%URL com extensão PHP que espera o arquivo%",
data: data,
contentType: false,
processData: false,
type: "POST",
success: function(data){
alert("arquivo rececido com sucesso");
}
});
remembering that you need to send a multipart/form-data
, if send as application/x-www-form-urlencoded
(a string similar to a queryString
), you will not be able to send a file to the server, so your PHP method must be ready to receive in this format (which is even the standard adopted by the web currently).
I’m also longing for a way to do that ;)
– Wallace Maxters
You cannot upload the video to the server inside a Formdata with AJAX?
– Tobias Mesquita
@Tobymosque, I don’t know how to use Formdata with Ajax applied to this case. You would have to create some example, or send some link that can help me understand how to use?
– Atoyansk
can create an example of how to send a stream to the server, the part of PHP you will have to unwind, but it will be the same thing to send a file in an inputfile.
– Tobias Mesquita
Yes, the PHP part I do without problems. Thank you!
– Atoyansk
@Atoyansk, ready, I made a response with the client-side part, unfortunately I can not help you with PHP.
– Tobias Mesquita