Created
March 2, 2022 10:49
-
-
Save YongmingZhao/4a3f592e9ab5b747aac21164a6a94287 to your computer and use it in GitHub Desktop.
Download a file with authentication
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<style> | |
</style> | |
<script src="https://code.jquery.com/jquery-3.6.0.js" | |
integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk=" crossorigin="anonymous"></script> | |
<script> | |
$(function () { | |
$('#download').on('click', function () { | |
const oReq = new XMLHttpRequest(); | |
oReq.open("GET", "http://jnr.host:9022/api/documents/project_information/f5f781568e8111ec9f8d5254006e0804", true); | |
oReq.setRequestHeader("Authentication", "Bearer 5667b7b0948411ecba11acbc328c771d"); | |
oReq.responseType = "blob"; | |
oReq.onload = function (oEvent) { | |
const a = $("<a style='display: none;'/>"); | |
const blob = oReq.response; | |
fileURL = URL.createObjectURL(blob); | |
a.attr("href", fileURL); | |
a.attr("download", "test.docx"); | |
$("body").append(a); | |
a[0].click(); | |
window.URL.revokeObjectURL(fileURL); | |
a.remove(); | |
}; | |
oReq.send(); | |
}) | |
}); | |
</script> | |
</head> | |
<body> | |
<button id="download" type="submit">Download!</button> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment