Skip to content

Commit

Permalink
add error check. (#972)
Browse files Browse the repository at this point in the history
Signed-off-by: Bo-Yi Wu <[email protected]>
  • Loading branch information
appleboy authored Jul 4, 2017
1 parent a625d91 commit 0f69e1e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
18 changes: 15 additions & 3 deletions upload-file/multiple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,28 @@ func main() {
email := c.PostForm("email")

// Multipart form
form, _ := c.MultipartForm()
form, err := c.MultipartForm()
if err != nil {
c.String(http.StatusBadRequest, fmt.Sprintf("get form err: %s", err.Error()))
return
}
files := form.File["files"]

for _, file := range files {
// Source
src, _ := file.Open()
src, err := file.Open()
if err != nil {
c.String(http.StatusBadRequest, fmt.Sprintf("file open err: %s", err.Error()))
return
}
defer src.Close()

// Destination
dst, _ := os.Create(file.Filename)
dst, err := os.Create(file.Filename)
if err != nil {
c.String(http.StatusBadRequest, fmt.Sprintf("Create file err: %s", err.Error()))
return
}
defer dst.Close()

// Copy
Expand Down
18 changes: 15 additions & 3 deletions upload-file/single/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,24 @@ func main() {
email := c.PostForm("email")

// Source
file, _ := c.FormFile("file")
src, _ := file.Open()
file, err := c.FormFile("file")
if err != nil {
c.String(http.StatusBadRequest, fmt.Sprintf("get form err: %s", err.Error()))
return
}
src, err := file.Open()
if err != nil {
c.String(http.StatusBadRequest, fmt.Sprintf("file open err: %s", err.Error()))
return
}
defer src.Close()

// Destination
dst, _ := os.Create(file.Filename)
dst, err := os.Create(file.Filename)
if err != nil {
c.String(http.StatusBadRequest, fmt.Sprintf("Create file err: %s", err.Error()))
return
}
defer dst.Close()

// Copy
Expand Down

0 comments on commit 0f69e1e

Please sign in to comment.