go
46 lines · 8 steps
Handling multi-file uploads in Gin
A Gin handler parses a multipart form, validates each file's size, saves them to disk, and reports what landed.
Explained by
highlit
1func UploadFiles(c *gin.Context) {
2 form, err := c.MultipartForm()
3 if err != nil {
4 c.JSON(http.StatusBadRequest, gin.H{"error": "invalid multipart form"})
5 return
6 }
7
8 files := form.File["documents"]
9 if len(files) == 0 {
10 c.JSON(http.StatusBadRequest, gin.H{"error": "no files provided"})
11 return
12 }
13
14 const maxSize = 10 << 20
15 uploaded := make([]gin.H, 0, len(files))
16
17 for _, file := range files {
18 if file.Size > maxSize {
19 c.JSON(http.StatusRequestEntityTooLarge, gin.H{
20 "error": fmt.Sprintf("%s exceeds the 10MB limit", file.Filename),
21 })
22 return
23 }
24
25 name := fmt.Sprintf("%d-%s", time.Now().UnixNano(), filepath.Base(file.Filename))
26 dst := filepath.Join("uploads", name)
27
28 if err := c.SaveUploadedFile(file, dst); err != nil {
29 c.JSON(http.StatusInternalServerError, gin.H{
30 "error": fmt.Sprintf("failed to save %s", file.Filename),
31 })
32 return
33 }
34
35 uploaded = append(uploaded, gin.H{
36 "original": file.Filename,
37 "stored": name,
38 "size": file.Size,
39 })
40 }
41
42 c.JSON(http.StatusCreated, gin.H{
43 "count": len(uploaded),
44 "files": uploaded,
45 })
46}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Validate the whole request shape before touching any file so bad requests fail fast with clear status codes.
- 2Renaming uploads with a timestamp and filepath.Base prevents collisions and strips client-supplied path components.
- 3Accumulating results in a slice lets you return a single structured summary after all files succeed.
Related explainers
go
package fetch import ( "context"
Concurrent API fetches with errgroup in Go
concurrency
goroutines
error-handling
Intermediate
8 steps
javascript
export function cloneState(state) { if (typeof structuredClone !== "function") { throw new Error("structuredClone is not available in this runtime"); }
Deep cloning with structuredClone
deep-copy
error-handling
immutability
Intermediate
7 steps
rust
use serde::Deserialize; use serde_json::Value; use std::collections::HashMap;
Modeling nested JSON with serde in Rust
deserialization
json
struct-mapping
Intermediate
9 steps
python
import threading import logging logger = logging.getLogger(__name__)
A self-rescheduling periodic task in Python
threading
scheduling
concurrency
Intermediate
6 steps
go
package pipeline import ( "context"
A cancelable worker pool in Go
concurrency
channels
worker-pool
Advanced
8 steps
ruby
class PurgeStaleExportsJob < ApplicationJob queue_as :maintenance retry_on ActiveRecord::Deadlocked, wait: :polynomially_longer, attempts: 5
Purging stale exports with an Active Job in Rails
background-jobs
batching
error-handling
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/handling-multi-file-uploads-in-gin-explained-go-6a95/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.