implemented file upload

This commit is contained in:
Djeeberjr 2021-08-06 13:59:01 +02:00
parent 713a96efc5
commit 318343e731
2 changed files with 26 additions and 3 deletions

View File

@ -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<boolean>[] = []
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 (
<div>
<DragAndDrop
handleDrop={handleDrop}
handleDrop={async (files)=>{
await handleDrop(files)
}}
>
<Breadcrum path={path} onDirClick={(newPath)=>{
setPath(newPath)

View File

@ -0,0 +1,17 @@
async function uploadFile(file:File,id: string): Promise<boolean> {
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