denoland / denoland/deploy_feedback

[Examples Gallery][New example]: Uploading files in Deno Deploy

Open
#328 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
No language data
Stars
79
Forks
5
PR merge metrics
No merged PRs in 30d

Description

Hi.

If possible, I would like to add the following example to the area `Examples Gallery` at the page https://deno.com/deploy/docs/examples:

```typescript
import * as path from "https://deno.land/std@0.173.0/path/mod.ts";
import { serve } from "https://deno.land/std@0.173.0/http/server.ts";
import { serveFile } from "https://deno.land/std@0.173.0/http/file_server.ts";

const UPLOADS_DIR = "./";

const html = `


Submit

`;

const headers = { "content-type": "text/html; charset=utf-8" };

async function handler(req: Request): Promise {
switch (req.method) {
case "GET": {
const url = new URL(req.url);
const fileName = url.searchParams.get("file");
if (fileName) {
return serveFile(req, path.join(UPLOADS_DIR, fileName));
}
return new Response(html, { headers });
}

case "POST": {
const form = await req.formData();
const upload = form.get("upload") as File;
if (!upload.name) {
return new Response("No file sent", { headers, status: 400 });
}
const file = await Deno.open(path.join(UPLOADS_DIR, upload.name), {
create: true,
write: true,
truncate: true,
});
upload.stream().pipeTo(file.writable);
return new Response(
`File saved ${upload.name}`,
{ headers }
);
}

default:
return new Response("Invalid method", { status: 405 });
}
}

serve(handler);
```

TIA

Contributor guide

No contributing guide indexed for this repository

Research direction

Start at the Examples Gallery page linked in the issue and inspect how existing examples are organized. Compare the supplied TypeScript upload example with those entries, then add it in the matching location and verify that the new example appears on the page and demonstrates file uploading in Deno Deploy.

Written by the indexing model from the issue text.

Assessment

Tech stack
deno, typescript
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.