Last active
November 11, 2022 15:51
-
-
Save appletreeat56/236708cc45f7eceb5f37e1ef17fa82bc 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
import axios from "axios"; | |
import { useEffect, useState } from "react"; | |
export default function FileUpload() { | |
const [file, setFile] = useState<any>(null); | |
const [uploadingStatus, setUploadingStatus] = useState<boolean>(false); | |
const uploadFile = async () => { | |
setUploadingStatus(true); | |
let { data } = await axios.post("/api/s3/upload", { | |
name: `canvasfun/${file.name}`, | |
type: file.type, | |
}); | |
const url = data.url; | |
await axios.put(url, file, { | |
headers: { | |
"Content-type": file.type, | |
"Access-Control-Allow-Origin": "*", | |
}, | |
}); | |
setUploadingStatus(false); | |
setFile(null); | |
}; | |
useEffect(() => { | |
if (file) { | |
const uploadedFileDetail = async () => await uploadFile(); | |
uploadedFileDetail(); | |
} | |
}, [file]); | |
return ( | |
<input | |
type="file" | |
accept="image/*" | |
name="image" | |
id="selectFile" | |
onChange={(e: any) => setFile(e.target.files[0])} | |
/> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment