file.go 425 B

1234567891011121314151617181920212223242526272829
  1. package services
  2. import (
  3. "io"
  4. "mime/multipart"
  5. "os"
  6. )
  7. // SaveUploadedFile 保存上传的本地文件
  8. func SaveUploadedFile(file *multipart.FileHeader, fpath string) (err error) {
  9. src, err := file.Open()
  10. if err != nil {
  11. return err
  12. }
  13. defer func() {
  14. _ = src.Close()
  15. }()
  16. out, err := os.Create(fpath)
  17. if err != nil {
  18. return err
  19. }
  20. defer func() {
  21. _ = out.Close()
  22. }()
  23. _, err = io.Copy(out, src)
  24. return
  25. }