var file = $('input[type="file"]').prop("files")[0];
var formData = new FormData();
formData.append('file-input', file);
$.ajax({
url: 'https://webdav.yandex.ru:443',
type: 'PUT',
data: formData,
path: 'images/',
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', 'OAuth <token>');
xhr.setRequestHeader('Expect', '100-continue');
xhr.setRequestHeader('Content-Type', 'application/binary');
xhr.setRequestHeader('Content-Length', file.size);
xhr.setRequestHeader('Host', 'webdav.yandex.ru');
xhr.setRequestHeader('Accept', '*/*');
},
success: function(data) {
console.dir(data);
}
});
// обработчик вешается на файловый инпут
document.getElementById('upload').addEventListener('change', function(e) {
var file = this.files[0];
var xhr = new XMLHttpRequest();
xhr.file = file; // not necessary if you create scopes like this
xhr.addEventListener('progress', function(e) {
var done = e.position || e.loaded, total = e.totalSize || e.total;
console.log('xhr progress: ' + (Math.floor(done/total*1000)/10) + '%');
}, false);
if ( xhr.upload ) {
xhr.upload.onprogress = function(e) {
var done = e.position || e.loaded, total = e.totalSize || e.total;
console.log('xhr.upload progress: ' + done + ' / ' + total + ' = ' + (Math.floor(done/total*1000)/10) + '%');
};
}
xhr.onreadystatechange = function(e) {
if ( 4 == this.readyState ) {
console.log(['xhr upload complete', e]);
}
};
xhr.open('put', url, true);
xhr.setRequestHeader("Content-Type","multipart/form-data");
xhr.send(file);
}, false);