From b27bf15abfa128147e32f471586fdfdf687dc139 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=A8=E6=9D=89?= Date: Sun, 24 Jan 2021 22:52:18 +0800 Subject: [PATCH] add file upload example --- examples/file_upload/README.md | 8 ++++++++ examples/file_upload/file | 1 + examples/file_upload/main.ts | 17 +++++++++++++++++ 3 files changed, 26 insertions(+) create mode 100644 examples/file_upload/README.md create mode 100644 examples/file_upload/file create mode 100644 examples/file_upload/main.ts diff --git a/examples/file_upload/README.md b/examples/file_upload/README.md new file mode 100644 index 0000000..8d3d225 --- /dev/null +++ b/examples/file_upload/README.md @@ -0,0 +1,8 @@ +## Usage + +``` +deno run --allow-net ./main.ts + +# in another terminal +curl http://localhost:8080/file -F "file=@./file" +``` diff --git a/examples/file_upload/file b/examples/file_upload/file new file mode 100644 index 0000000..c57eff5 --- /dev/null +++ b/examples/file_upload/file @@ -0,0 +1 @@ +Hello World! \ No newline at end of file diff --git a/examples/file_upload/main.ts b/examples/file_upload/main.ts new file mode 100644 index 0000000..5395222 --- /dev/null +++ b/examples/file_upload/main.ts @@ -0,0 +1,17 @@ +import type { FormFile } from "../../vendor/https/deno.land/std/mime/multipart.ts"; +import { Application } from "../../mod.ts"; +import { decoder } from "../../vendor/https/deno.land/std/encoding/utf8.ts"; + +const app = new Application(); + +app.start({ port: 8080 }); + +console.log(`server listening on http://localhost:8080`); + +app.post("/file", async (c) => { + const { file } = await c.body as { file: FormFile }; + return { + name: file.filename, + content: decoder.decode(file.content), + }; +});