go get -v github.com/LyricTian/fuh
package main
import (
"context"
"encoding/json"
"net/http"
"path/filepath"
"github.com/LyricTian/fuh"
)
func main() {
upl := fuh.NewUploader(&fuh.Config{
BasePath: "attach",
SizeLimit: 1 << 20,
}, fuh.NewFileStore())
http.HandleFunc("/fileupload", func(w http.ResponseWriter, r *http.Request) {
ctx := fuh.NewFileNameContext(context.Background(), func(ci fuh.ContextInfo) string {
return filepath.Join(ci.BasePath(), ci.FileName())
})
finfos, err := upl.Upload(ctx, r, "file")
if err != nil {
w.WriteHeader(500)
return
}
json.NewEncoder(w).Encode(finfos)
})
http.ListenAndServe(":8080", nil)
}
$ go build server.go
$ ./server
- Custom file name
- Custom file size limit
- Supports storage extensions
- Context support
Copyright (c) 2017 Lyric