From 318343e731e9c5c4e774dcb7bdb1beecdfb1a393 Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Fri, 6 Aug 2021 13:59:01 +0200 Subject: [PATCH] implemented file upload --- src/components/FileBrowser.tsx | 12 +++++++++--- src/functions/uploadFile.ts | 17 +++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 src/functions/uploadFile.ts diff --git a/src/components/FileBrowser.tsx b/src/components/FileBrowser.tsx index e1889ed..0355bf4 100644 --- a/src/components/FileBrowser.tsx +++ b/src/components/FileBrowser.tsx @@ -1,5 +1,6 @@ import React from "react" import { useState } from "react" +import uploadFile from "../functions/uploadFile" import { useOpenDirQuery } from "../generated/graphql" import Breadcrum from "./Breadcrum" import DragAndDrop from "./DragAndDrop" @@ -17,17 +18,22 @@ const FileBrowser: React.FC = () => { } }) - function handleDrop(files:FileList) { + async function handleDrop(files:FileList) { + const wait: Promise[] = [] for (let i = 0; i < files.length; i++) { const file = files[i] - console.debug(file) // TODO + wait.push(uploadFile(file, path + file.name)) } + + await Promise.all(wait) } return (
{ + await handleDrop(files) + }} > { setPath(newPath) diff --git a/src/functions/uploadFile.ts b/src/functions/uploadFile.ts new file mode 100644 index 0000000..c3b49fb --- /dev/null +++ b/src/functions/uploadFile.ts @@ -0,0 +1,17 @@ +async function uploadFile(file:File,id: string): Promise { + + console.debug(file) + + + const res = await fetch(`/api/file?${new URLSearchParams({id:id}).toString()}`,{ + method: "POST", + headers: { + "Content-Type": file.type + }, + body: file + }) + + return res.status == 200 +} + +export default uploadFile