httprint/dist/static/js/httprint.js

62 lines
2 KiB
JavaScript
Raw Normal View History

2019-08-14 22:19:44 +02:00
function uploadFile() {
let photo = document.getElementById("upload-file").files[0];
2019-08-16 20:54:49 +02:00
let copies = document.getElementById("copies").value;
2019-08-14 22:19:44 +02:00
let formData = new FormData();
formData.append("file", photo);
2019-08-15 13:34:44 +02:00
formData.append("copies", copies);
2019-08-16 20:54:49 +02:00
var uploadField = document.getElementById("upload-file");
2019-08-14 22:19:44 +02:00
fetch("/api/upload", {method: "POST", body: formData})
.then(function(response) {
return response.json();
})
.then(function(reply) {
if (reply && !reply.error) {
2019-08-16 23:08:20 +02:00
$.toast({
text: reply.message || "file sent to printer",
heading: "DONE!",
icon: "success",
showHideTransition: "fade",
allowToastClose: true,
hideAfter: false,
stack: 5,
position: "top-center"
2019-08-14 22:19:44 +02:00
});
2019-08-17 14:40:28 +02:00
uploadField.value = null;
copies.value = 1;
2019-08-14 22:19:44 +02:00
} else {
2019-08-16 23:08:20 +02:00
$.toast({
text: reply.message || "unable to print file",
heading: "ERROR!",
icon: "error",
showHideTransition: "fade",
allowToastClose: true,
hideAfter: 5000,
stack: 5,
position: "top-center"
2019-08-14 22:19:44 +02:00
});
}
})
.catch(function(err) {
2019-08-16 20:54:49 +02:00
console.log(err);
2019-08-16 23:08:20 +02:00
$.toast({
text: "failed to send file",
heading: "ERROR!",
icon: "error",
showHideTransition: "fade",
allowToastClose: true,
hideAfter: 5000,
stack: 5,
position: "top-center"
2019-08-14 22:19:44 +02:00
});
});
}
2019-08-16 20:54:49 +02:00
document.addEventListener("DOMContentLoaded", function(event) {
let pbutton = document.getElementById("print-btn");
pbutton.addEventListener("click", function(event) {
uploadFile();
});
});