-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from nyaomaru/deno-study-2
study: deno define routes and forms
- Loading branch information
Showing
4 changed files
with
64 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { Handlers, type PageProps } from "$fresh/server.ts"; | ||
|
||
interface Props { | ||
message: string | null; | ||
} | ||
|
||
export const handler: Handlers<Props> = { | ||
async GET(req, ctx) { | ||
return await ctx.render({ | ||
message: null, | ||
}); | ||
}, | ||
async POST(req, ctx) { | ||
const form = await req.formData(); | ||
const file = form.get("my-file") as File; | ||
|
||
if (!file) { | ||
return ctx.render({ | ||
message: `Please try again`, | ||
}); | ||
} | ||
|
||
const name = file.name; | ||
const contents = await file.text(); | ||
|
||
console.log(contents); | ||
|
||
return ctx.render({ | ||
message: `${name} uploaded!`, | ||
}); | ||
}, | ||
}; | ||
|
||
export default function Upload(props: PageProps<Props>) { | ||
const { message } = props.data; | ||
return ( | ||
<> | ||
<form method="post" encType="multipart/form-data"> | ||
<input type="file" name="my-file" /> | ||
<button type="submit">Upload</button> | ||
</form> | ||
{message ? <p>{message}</p> : null} | ||
</> | ||
); | ||
} |