| 1 |
<!doctype html> |
| 2 |
<html> |
| 3 |
|
| 4 |
<head> |
| 5 |
<title>axios - file upload example</title> |
| 6 |
<link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" /> |
| 7 |
</head> |
| 8 |
|
| 9 |
<body class="container"> |
| 10 |
<h1>file upload</h1> |
| 11 |
|
| 12 |
<form role="form" class="form" onsubmit="return false;"> |
| 13 |
<div class="form-group"> |
| 14 |
<label for="file">File</label> |
| 15 |
<input id="file" type="file" class="form-control" /> |
| 16 |
</div> |
| 17 |
<button id="upload" type="button" class="btn btn-primary">Upload</button> |
| 18 |
</form> |
| 19 |
|
| 20 |
<div id="output" class="container"></div> |
| 21 |
|
| 22 |
<script src="/axios.min.js"></script> |
| 23 |
<script> |
| 24 |
(function () { |
| 25 |
var output = document.getElementById('output'); |
| 26 |
document.getElementById('upload').onclick = function () { |
| 27 |
var data = new FormData(); |
| 28 |
data.append('foo', 'bar'); |
| 29 |
data.append('file', document.getElementById('file').files[0]); |
| 30 |
|
| 31 |
var config = { |
| 32 |
onUploadProgress: function (progressEvent) { |
| 33 |
var percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total); |
| 34 |
output.className = 'container'; |
| 35 |
output.textContent = 'Upload progress: ' + percentCompleted + '%'; |
| 36 |
} |
| 37 |
}; |
| 38 |
|
| 39 |
axios.put('/upload/server', data, config) |
| 40 |
.then(function (res) { |
| 41 |
output.className = 'container'; |
| 42 |
output.textContent = res.data; |
| 43 |
}) |
| 44 |
.catch(function (err) { |
| 45 |
output.className = 'container text-danger'; |
| 46 |
output.textContent = err.message; |
| 47 |
}); |
| 48 |
}; |
| 49 |
})(); |
| 50 |
</script> |
| 51 |
</body> |
| 52 |
|
| 53 |
</html> |