SetFile set up a multipart form from file path to upload, which read file from filePath automatically to upload.
(paramName, filePath string)
| 299 | // SetFile set up a multipart form from file path to upload, |
| 300 | // which read file from filePath automatically to upload. |
| 301 | func (r *Request) SetFile(paramName, filePath string) *Request { |
| 302 | file, err := os.Open(filePath) |
| 303 | if err != nil { |
| 304 | r.client.log.Errorf("failed to open %s: %v", filePath, err) |
| 305 | r.appendError(err) |
| 306 | return r |
| 307 | } |
| 308 | fileInfo, err := os.Stat(filePath) |
| 309 | if err != nil { |
| 310 | r.client.log.Errorf("failed to stat file %s: %v", filePath, err) |
| 311 | r.appendError(err) |
| 312 | return r |
| 313 | } |
| 314 | r.isMultiPart = true |
| 315 | return r.SetFileUpload(FileUpload{ |
| 316 | ParamName: paramName, |
| 317 | FileName: filepath.Base(filePath), |
| 318 | GetFileContent: func() (io.ReadCloser, error) { |
| 319 | if r.RetryAttempt > 0 { |
| 320 | file, err = os.Open(filePath) |
| 321 | if err != nil { |
| 322 | return nil, err |
| 323 | } |
| 324 | } |
| 325 | return file, nil |
| 326 | }, |
| 327 | FileSize: fileInfo.Size(), |
| 328 | }) |
| 329 | } |
| 330 | |
| 331 | var ( |
| 332 | errMissingParamName = errors.New("missing param name in multipart file upload") |