Created
October 10, 2020 02:59
-
-
Save zvalentine22/bda7d1379f796a514fb7b32a317d9e95 to your computer and use it in GitHub Desktop.
snapmaker2.0 jquery upload without luban
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> | |
</head> | |
<body> | |
<style> | |
#fileselector, #upload {display: none;} | |
</style> | |
<form id="fileform"> | |
<input type="button" id="connect" value="Connect"> | |
<input type="file" id="fileselector" accept=".gcode"><br> | |
<input type="button" id="upload" value="Upload"> | |
</form> | |
<script type="text/javascript"> | |
// Set Printer IP:------------- | |
var printer_ip = "192.168.0.47"; | |
// ---------------------------- | |
var base_url = "http://" + printer_ip + ":8080/api/v1"; | |
var token; | |
// request access token from printer | |
$("#connect").click(function(){ | |
$.ajax({ | |
url: base_url + "/connect", | |
type: "post", | |
success: function(data, status){ | |
console.log("Token: " + data.token + "\nStatus: " + status) | |
token = data.token; | |
$("#fileselector").show(); | |
$("#connect").hide(); | |
}, | |
error: function(data, status){ | |
alert("Data: " + data.responseText + "\nStatus: " + status) | |
} | |
}); | |
}); | |
// Reveal upload button upon file selection | |
$("#fileselector").change(function(){ | |
$("#upload").show(); | |
}); | |
// Attempt to upload file | |
$("#upload").click(function(){ | |
if (token){ | |
var data = new FormData(); | |
data.append('token',token); | |
data.append('file', $('#fileselector')[0].files[0]); | |
$.ajax({ | |
url: base_url + '/upload', | |
type: 'post', | |
data: data, | |
processData: false, | |
contentType: false, | |
success: function(data, status){ | |
console.log(data.response); | |
alert("Data: " + data.responseText + "\nStatus: " + status) | |
}, | |
error: function(data, status){ | |
alert("Data: " + data.responseText + "\nStatus: " + status) | |
} | |
}); | |
}; | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment