Skip to content

Commit

Permalink
S3 file upload (#4)
Browse files Browse the repository at this point in the history
* Implemented S3 upload

* Updated README

* Updated README

* Cleaned up dependencies

* Fixed dependencies

* Refactored from useActionData to useFetcher
  • Loading branch information
TobiasGeiselmann authored Jul 21, 2022
1 parent 3dd7190 commit 19c1495
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions examples/file-and-s3-upload/app/routes/s3-upload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
unstable_createMemoryUploadHandler as createMemoryUploadHandler,
unstable_parseMultipartFormData as parseMultipartFormData,
} from "@remix-run/node";
import { Form, useActionData } from "@remix-run/react";
import { useFetcher } from "@remix-run/react";
import { s3UploadHandler } from "~/utils/s3.server";

type ActionData = {
Expand All @@ -25,7 +25,7 @@ export const action: ActionFunction = async ({ request }) => {
console.log(imgDesc)
if (!imgSrc) {
return json({
error: "Something went wrong while uploading",
errorMsg: "Something went wrong while uploading",
});
}
return json({
Expand All @@ -35,25 +35,26 @@ export const action: ActionFunction = async ({ request }) => {
};

export default function Index() {
const data = useActionData<ActionData>();
const fetcher = useFetcher<ActionData>();
return (
<>
<Form method="post" encType="multipart/form-data">
<fetcher.Form method="post" encType="multipart/form-data">
<label htmlFor="img-field">Image to upload</label>
<input id="img-field" type="file" name="img" accept="image/*" />
<label htmlFor="img-desc">Image description</label>
<input id="img-desc" type="text" name="desc" />
<button type="submit">Upload to S3</button>
</Form>
{data?.errorMsg && <h2>{data.errorMsg}</h2>}
{data?.imgSrc && (
<>
</fetcher.Form>
{fetcher.type === "done" ? (
fetcher.data.errorMsg ? (
<h2>{fetcher.data.errorMsg}</h2>
) : (
<>
<div>File has been uploaded to S3 and is available under the following URL (if the bucket has public access enabled):</div>
<div>{data.imgSrc}</div>
<img src={data.imgSrc} alt={data.imgDesc || "Uploaded image from S3"} />

<div>{fetcher.data.imgSrc}</div>
<img src={fetcher.data.imgSrc} alt={fetcher.data.imgDesc || "Uploaded image from S3"} />
</>
)}
)) : null}
</>
);
}

0 comments on commit 19c1495

Please sign in to comment.