From a604ec44d7f7e673a75e91640025843b8886a894 Mon Sep 17 00:00:00 2001 From: Janne Koschinski <janne@kuschku.de> Date: Sun, 18 Mar 2018 18:06:28 +0100 Subject: [PATCH] Improved upload API --- templates/upload.html | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/templates/upload.html b/templates/upload.html index e875003..06efa1e 100644 --- a/templates/upload.html +++ b/templates/upload.html @@ -27,15 +27,38 @@ <input type="submit" value="Upload"/> <p class="upload-label">Uploading…</p> </form> + <div class="uploading images"></div> </div> <script> const form = document.querySelector("form.upload"); const element = document.querySelector("form.upload input[type=file]"); - /* + const results = document.querySelector(".uploading.images"); element.addEventListener("change", () => { + for (let file of element.files) { + const reader = new FileReader(); + reader.addEventListener("load", (e) => { + const node = document.createElement("div"); + node.classList.add("uploading"); + node.classList.add("image"); + const img = document.createElement("img"); + img.src = e.target.result; + node.appendChild(img); + results.appendChild(node); + fetch({ + url: "/upload", + credentials: "include" + }).then((response) => { + return response.json() + }).then((json) => { + const text = document.createElement("pre"); + text.innerText = JSON.stringify(json); + node.appendChild(text); + }); + }); + reader.readAsDataURL(file); + } form.submit(); form.classList.add("submitted"); }) - */ </script> {{end}} \ No newline at end of file -- GitLab