package requests import ( "io" "log" "os" "path/filepath" ) // UploadFile 上传文件的结构 type UploadFile struct { FileName string FieldName string FileReaderCloser io.ReadCloser } // UploadFileFromPath 从本地文件获取上传文件 func UploadFileFromPath(fileName string) (*UploadFile, error) { fd, err := os.Open(fileName) if err != nil { return nil, err } return &UploadFile{FileReaderCloser: fd, FileName: fileName}, nil } // UploadFileFromGlob 根据Glob从本地文件获取上传文件 func UploadFileFromGlob(glob string) ([]*UploadFile, error) { files, err := filepath.Glob(glob) if err != nil { return nil, err } if len(files) == 0 { log.Println("UploadFileFromGlob: len(files) == 0") } var ufiles []*UploadFile for _, f := range files { if s, err := os.Stat(f); err != nil || s.IsDir() { continue } fd, err := os.Open(f) if err != nil { log.Println(fd.Name(), err) } else { ufiles = append(ufiles, &UploadFile{FileReaderCloser: fd, FileName: filepath.Base(fd.Name())}) } } return ufiles, nil }