Skip to content

Commit

Permalink
return json response when upload
Browse files Browse the repository at this point in the history
  • Loading branch information
codeskyblue committed Jun 14, 2017
1 parent 22c83d8 commit 3ff2620
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions httpstaticserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,12 @@ func (s *HTTPStaticServer) hUpload(w http.ResponseWriter, req *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer file.Close()
defer req.MultipartForm.RemoveAll() // Seen from go source code, req.MultipartForm not nil after call FormFile(..)
dst, err := os.Create(filepath.Join(dirpath, header.Filename))
defer func() {
file.Close()
req.MultipartForm.RemoveAll() // Seen from go source code, req.MultipartForm not nil after call FormFile(..)
}()
dstPath := filepath.Join(dirpath, header.Filename)
dst, err := os.Create(dstPath)
if err != nil {
log.Println("Create file:", err)
http.Error(w, "File create "+err.Error(), http.StatusInternalServerError)
Expand All @@ -170,7 +173,11 @@ func (s *HTTPStaticServer) hUpload(w http.ResponseWriter, req *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write([]byte("Upload success"))
w.Header().Set("Content-Type", "application/json;charset=utf-8")
json.NewEncoder(w).Encode(map[string]interface{}{
"success": true,
"destination": dstPath,
})
}

type FileJSONInfo struct {
Expand Down

0 comments on commit 3ff2620

Please sign in to comment.