file-upload

command
v1.8.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 10, 2026 License: MIT Imports: 4 Imported by: 0

README ΒΆ

File Upload Example

This example demonstrates file upload and download in DotWeb.

Features

  • Single file upload
  • Multiple file upload
  • File download
  • List uploaded files
  • Delete files
  • File size limits

Running

cd example/file-upload
go run main.go

Testing

Upload Single File
# Create a test file
echo "Hello, DotWeb!" > test.txt

# Upload file
curl -F '[email protected]' http://localhost:8080/upload
# Output:
# βœ… File uploaded!
# πŸ“ Name: test.txt
# πŸ“Š Size: 14 bytes
# πŸ“ Path: ./uploads/test.txt
Upload Multiple Files
curl -F '[email protected]' -F '[email protected]' http://localhost:8080/upload/multiple
# Output:
# Uploaded 2 files:
# [βœ… file1.txt βœ… file2.txt]
List Files
curl http://localhost:8080/files
# Output:
# πŸ“‚ Uploaded files:
# πŸ“ test.txt (14 bytes)
Download File
curl http://localhost:8080/download/test.txt -o downloaded.txt
Delete File
curl -X DELETE http://localhost:8080/files/test.txt
# Output: βœ… File deleted: test.txt

API Reference

Upload File
// Get single file
file, header, err := ctx.Request().FormFile("file")

// Get file content
data, err := io.ReadAll(file)

// Get file name
filename := header.Filename
Upload Multiple Files
// Parse multipart form
err := ctx.Request().ParseMultipartForm(32 << 20) // 32MB

// Get all files
files := ctx.Request().MultipartForm.File["files"]
Download File
// Set headers for download
ctx.Response().Header().Set("Content-Disposition", "attachment; filename="+filename)
ctx.Response().Header().Set("Content-Type", "application/octet-stream")

// Send file data
data, _ := os.ReadFile(filePath)
ctx.Write(200, data)

Configuration

Set Max Body Size
// 10MB limit
app.HttpServer.SetMaxBodySize(10 * 1024 * 1024)

// Unlimited
app.HttpServer.SetMaxBodySize(-1)

File Upload Helper

DotWeb provides a built-in upload file helper:

// Using UploadFile helper
uploadFile := ctx.Request().UploadFile("file")
if uploadFile != nil {
    filename := uploadFile.Filename
    data := uploadFile.Data  // []byte
    size := len(data)
}

Common Patterns

Validate File Type
func isValidFileType(filename string) bool {
    ext := strings.ToLower(filepath.Ext(filename))
    allowed := []string{".jpg", ".jpeg", ".png", ".gif", ".pdf"}
    for _, a := range allowed {
        if ext == a {
            return true
        }
    }
    return false
}
Generate Unique Filename
import "github.com/google/uuid"

func uniqueFilename(filename string) string {
    ext := filepath.Ext(filename)
    return uuid.New().String() + ext
}
Check File Size
func checkFileSize(size int64, maxSize int64) bool {
    return size <= maxSize
}

Notes

  • Always validate uploaded files
  • Set appropriate max body size
  • Use unique filenames to avoid conflicts
  • Check file types for security
  • Clean up old files periodically

Documentation ΒΆ

Overview ΒΆ

Package main demonstrates file upload and download in DotWeb. Run: go run main.go Test: curl -F "[email protected]" http://localhost:8080/upload

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL