Created
May 13, 2021 03:06
-
-
Save dev-sankhadip/395734400703f3420f75f726c7495a47 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 lang="en"> | |
<head> | |
<meta charset="UTF-8" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
<title>Document</title> | |
</head> | |
<body> | |
<input type="file" id="file-input" accept="image/*" /> | |
<script> | |
const inputFile = document.getElementById("file-input"); | |
inputFile.onchange = function (e) { | |
const file = e.target.files[0]; | |
const fileName = e.target.files[0].name; | |
const reader = new FileReader(); | |
reader.readAsDataURL(file); | |
reader.onload = function () { | |
fetch("/store", { | |
method: "POST", | |
body: JSON.stringify({ image: reader.result, fileName }), | |
headers: { | |
"Content-Type": "application/json", | |
}, | |
}) | |
.then(async (res) => { | |
inputFile.remove(); | |
const result = await res.json(); | |
let a = document.createElement("a"); | |
a.href = `/image/${result.id}`; | |
a.click(); | |
}) | |
.catch((err) => { | |
console.log(err); | |
}); | |
}; | |
reader.onerror = function () { | |
console.log(reader.error); | |
}; | |
}; | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment