httprint/dist/static/js/httprint.js

52 lines
1.6 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) {
iziToast.success({
title: "DONE!",
2019-08-16 20:54:49 +02:00
message: reply.message || "file sent to printer",
position: "topCenter",
2019-08-15 10:41:24 +02:00
timeout: false,
closeOnEscape: true,
2019-08-14 22:19:44 +02:00
layout: 2
});
} else {
iziToast.error({
title: "ERROR!",
2019-08-16 20:54:49 +02:00
message: reply.message || "unable to print file",
position: "topCenter",
2019-08-14 22:19:44 +02:00
layout: 2
});
}
2019-08-15 13:34:44 +02:00
uploadField.value = null;
2019-08-14 22:19:44 +02:00
})
.catch(function(err) {
2019-08-16 20:54:49 +02:00
console.log(err);
2019-08-14 22:19:44 +02:00
iziToast.error({
title: "ERROR!",
message: "failed to send file",
2019-08-16 20:54:49 +02:00
position: "topCenter",
2019-08-14 22:19:44 +02:00
layout: 2
});
2019-08-15 13:34:44 +02:00
uploadField.value = null;
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();
});
});