feat(context): add SaveUploadedFile func. (#1022)
* feat(context): add SaveUploadedFile func. * feat(context): update multiple upload examples. * style(example): fix gofmt * fix(example): add missing return
This commit is contained in:
		@ -2,9 +2,7 @@ package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"io"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"os"
 | 
			
		||||
 | 
			
		||||
	"github.com/gin-gonic/gin"
 | 
			
		||||
)
 | 
			
		||||
@ -25,24 +23,10 @@ func main() {
 | 
			
		||||
		files := form.File["files"]
 | 
			
		||||
 | 
			
		||||
		for _, file := range files {
 | 
			
		||||
			// Source
 | 
			
		||||
			src, err := file.Open()
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				c.String(http.StatusBadRequest, fmt.Sprintf("file open err: %s", err.Error()))
 | 
			
		||||
			if err := c.SaveUploadedFile(file, file.Filename); err != nil {
 | 
			
		||||
				c.String(http.StatusBadRequest, fmt.Sprintf("upload file err: %s", err.Error()))
 | 
			
		||||
				return
 | 
			
		||||
			}
 | 
			
		||||
			defer src.Close()
 | 
			
		||||
 | 
			
		||||
			// Destination
 | 
			
		||||
			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
 | 
			
		||||
			io.Copy(dst, src)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		c.String(http.StatusOK, fmt.Sprintf("Uploaded successfully %d files with fields name=%s and email=%s.", len(files), name, email))
 | 
			
		||||
 | 
			
		||||
@ -2,9 +2,7 @@ package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"io"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"os"
 | 
			
		||||
 | 
			
		||||
	"github.com/gin-gonic/gin"
 | 
			
		||||
)
 | 
			
		||||
@ -22,23 +20,11 @@ func main() {
 | 
			
		||||
			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()))
 | 
			
		||||
 | 
			
		||||
		if err := c.SaveUploadedFile(file, file.Filename); err != nil {
 | 
			
		||||
			c.String(http.StatusBadRequest, fmt.Sprintf("upload file err: %s", err.Error()))
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		defer src.Close()
 | 
			
		||||
 | 
			
		||||
		// Destination
 | 
			
		||||
		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
 | 
			
		||||
		io.Copy(dst, src)
 | 
			
		||||
 | 
			
		||||
		c.String(http.StatusOK, fmt.Sprintf("File %s uploaded successfully with fields name=%s and email=%s.", file.Filename, name, email))
 | 
			
		||||
	})
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user