forked from bingoohuang/xlsx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
49 lines (38 loc) · 1.19 KB
/
http.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package xlsx
import (
"log"
"mime"
"net/http"
"github.com/unidoc/unioffice/spreadsheet"
)
// WithUpload defines the input excel file for reading.
func WithUpload(r *http.Request, filenameKey string) OptionFn {
wb, err := parseUploadFile(r, filenameKey)
if err != nil {
log.Printf("W! failed to open template excel %v", err)
return nil
}
return func(o *Option) { o.Workbook = wb }
}
// nolint:gomnd
func parseUploadFile(r *http.Request, filenameKey string) (*spreadsheet.Workbook, error) {
_ = r.ParseMultipartForm(32 << 20) // limit your max input length!
file, header, err := r.FormFile(filenameKey)
if err != nil {
return nil, err
}
defer file.Close()
return spreadsheet.Read(file, header.Size)
}
// Download downloads the excels file in the http response.
func (x *Xlsx) Download(w http.ResponseWriter, filename string) error {
h := w.Header().Set
h("Content-Disposition", mime.FormatMediaType("attachment", map[string]string{"filename": filename}))
h("Content-Description", "File Transfer")
h("Content-Type", "application/octet-stream")
h("Content-Transfer-Encoding", "binary")
h("Expires", "0")
h("Cache-Control", "must-revalidate")
h("Pragma", "public")
return x.Save(w)
}