-
Notifications
You must be signed in to change notification settings - Fork 0
/
webFuncs.go
60 lines (55 loc) · 1.15 KB
/
webFuncs.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
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"crypto/md5"
"fmt"
"html/template"
"io"
"net/http"
"os"
"strconv"
"time"
)
func upload(w http.ResponseWriter, r *http.Request) {
fmt.Println("method:", r.Method)
if r.Method == "GET" {
crutime := time.Now().Unix()
h := md5.New()
_, err := io.WriteString(h, strconv.FormatInt(crutime, 10))
if err != nil {
fmt.Println(err)
os.Exit(1)
}
token := fmt.Sprintf("%x", h.Sum(nil))
t, err := template.ParseFiles("upload.gtpl")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
err = t.Execute(w, token)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
} else {
err := r.ParseMultipartForm(32 << 20)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
file, _, err := r.FormFile("uploadfile")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
patClass := r.PostFormValue("patClass")
fmt.Printf("PatClass is %s\n", patClass)
buffer := convertCsvToXML(file, patClass)
w.Header().Set("Content-Disposition", "attachment; filename=patient.xml")
w.Header().Set("Content-Type", r.Header.Get("Content-Type"))
_, err = io.Copy(w, buffer)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
}