Skip to content

Commit 8693cd0

Browse files
Copilotringabout
andcommitted
Add example demonstrating multiple file upload functionality
Co-authored-by: ringabout <43030857+ringabout@users.noreply.github.com>
1 parent a16f5aa commit 8693cd0

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Multiple File Upload Example
2+
3+
This example demonstrates how to handle multiple file uploads from a single HTML input element using Prologue's new `getUploadFiles` API.
4+
5+
## What's New
6+
7+
Prologue now supports retrieving multiple files uploaded from a single input element with the `multiple` attribute.
8+
9+
### New API
10+
11+
- **`getUploadFiles(name: string): seq[UploadFile]`**: Returns all uploaded files with the given input name
12+
- **`getUploadFile(name: string): UploadFile`**: Returns the first uploaded file (backward compatible)
13+
14+
## HTML Form
15+
16+
```html
17+
<form method="post" action="/upload" enctype="multipart/form-data">
18+
<input type="file" name="files" multiple />
19+
<input type="submit" value="Upload" />
20+
</form>
21+
```
22+
23+
## Handler Code
24+
25+
```nim
26+
proc uploadFiles(ctx: Context) {.async.} =
27+
let files = ctx.getUploadFiles("files")
28+
29+
for file in files:
30+
echo "Filename: ", file.filename
31+
echo "Size: ", file.body.len, " bytes"
32+
# Optionally save the file
33+
# file.save("uploads/")
34+
```
35+
36+
## Running the Example
37+
38+
```bash
39+
nim c -r examples/multifile_upload/app.nim
40+
```
41+
42+
Then open your browser to `http://localhost:8080` and select multiple files to upload.

examples/multifile_upload/app.nim

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import ../../src/prologue
2+
import std/strformat
3+
4+
proc uploadForm(ctx: Context) {.async.} =
5+
## Serve the HTML form for uploading multiple files
6+
await ctx.staticFileResponse("examples/multifile_upload/upload.html", "")
7+
8+
proc uploadFiles(ctx: Context) {.async.} =
9+
## Handle multiple file uploads from a single input element
10+
let files = ctx.getUploadFiles("files")
11+
12+
var response = "<html><head><title>Upload Results</title></head><body>"
13+
response.add("<h1>Uploaded Files</h1>")
14+
response.add(fmt"<p>Total files uploaded: {files.len}</p>")
15+
response.add("<ul>")
16+
17+
for i, file in files:
18+
response.add(fmt"<li>File {i+1}: {file.filename} ({file.body.len} bytes)</li>")
19+
# Optionally save each file
20+
# file.save("uploads/")
21+
22+
response.add("</ul>")
23+
response.add("<a href='/'>Upload more files</a>")
24+
response.add("</body></html>")
25+
26+
resp response
27+
28+
proc main() =
29+
let settings = newSettings()
30+
var app = newApp(settings)
31+
32+
app.get("/", uploadForm)
33+
app.post("/upload", uploadFiles)
34+
35+
app.run()
36+
37+
when isMainModule:
38+
main()
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Multiple File Upload Example</title>
5+
<style>
6+
body {
7+
font-family: Arial, sans-serif;
8+
max-width: 600px;
9+
margin: 50px auto;
10+
padding: 20px;
11+
}
12+
.upload-form {
13+
border: 2px dashed #ccc;
14+
border-radius: 10px;
15+
padding: 30px;
16+
text-align: center;
17+
}
18+
input[type="file"] {
19+
margin: 20px 0;
20+
}
21+
input[type="submit"] {
22+
background-color: #4CAF50;
23+
color: white;
24+
padding: 10px 20px;
25+
border: none;
26+
border-radius: 4px;
27+
cursor: pointer;
28+
font-size: 16px;
29+
}
30+
input[type="submit"]:hover {
31+
background-color: #45a049;
32+
}
33+
</style>
34+
</head>
35+
<body>
36+
<h1>Multiple File Upload Example</h1>
37+
<div class="upload-form">
38+
<h2>Select Multiple Files</h2>
39+
<form method="post" action="/upload" enctype="multipart/form-data">
40+
<input type="file" name="files" multiple required />
41+
<br>
42+
<input type="submit" value="Upload Files" />
43+
</form>
44+
</div>
45+
<p><strong>Note:</strong> You can select multiple files at once using Ctrl+Click (or Cmd+Click on Mac) in the file picker dialog.</p>
46+
</body>
47+
</html>

0 commit comments

Comments
 (0)