(25 июн 2016, 21:56) (
0/
0)
[
0]
Денис Павлик,
Загрузка файлов? Я так делал:
[code]
<script>
function log(html) {
document.getElementById('log').innerHTML = html;
}
function onSuccess() {
log('success');
}
function onError() {
log('error');
}
function onProgress(loaded, total) {
log(loaded + ' / ' + total);
}
document.forms.upload.onsubmit = function() {
var file = this.elements.myfile.files[0];
if (file) {
upload(file);
}
return false;
}
function upload(file) {
var xhr = new XMLHttpRequest();
var formData = new FormData();
var form = document.forms.upload;
xhr.onload = xhr.onerror = function() {
if (this.status == 200) {
var o = JSON.parse(xhr.responseText);
if(o.type == 'success') {
location.href = '/file/' + o.id;
}
if(o.type == 'error') {
log("<b>Ошибка:</b> " + o.content);
}
} else {
log("Ошибка " + this.status);
}
};
xhr.upload.onprogress = function(event) {
var onePerc = event.total/100;
var loaded = event.loaded / onePerc;
loaded = Math.floor(loaded);
log('<progress id="progressbar" value="' + loaded + '" max="100"></progress> <b>Идёт загрузка: ' + loaded + '%</b>');
}
xhr.open("POST", "/modules/upload/handler.php", true);
formData.append("myfile", file);
formData.append("name", form.elements.name.value);
formData.append("description", form.elements.description.value);
formData.append("password", form.elements.password.value);
xhr.send(formData);
}
</script>
[/code]