Skip to content

Instantly share code, notes, and snippets.

@dev-sankhadip
Created May 13, 2021 03:06
Show Gist options
  • Save dev-sankhadip/395734400703f3420f75f726c7495a47 to your computer and use it in GitHub Desktop.
Save dev-sankhadip/395734400703f3420f75f726c7495a47 to your computer and use it in GitHub Desktop.
<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