Skip to content

Commit a76c2fb

Browse files
FlamingTreelevigross
authored andcommitted
add FieldName to FileUpload (levigross#27)
1 parent e9e437f commit a76c2fb

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

file_upload.go

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ type FileUpload struct {
1515

1616
// FileContents is happy as long as you pass it a io.ReadCloser (which most file use anyways)
1717
FileContents io.ReadCloser
18+
19+
// FieldName is form field name
20+
FieldName string
1821
}
1922

2023
// FileUploadFromDisk allows you to create a FileUpload struct slice by just specifying a location on the disk

request.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -263,13 +263,17 @@ func createMultiPartPostRequest(httpMethod, userURL string, ro *RequestOptions)
263263
return nil, errors.New("grequests: Pointer FileContents cannot be nil")
264264
}
265265

266-
fileName := "file"
267-
268-
if len(ro.Files) > 1 {
269-
fileName = strings.Join([]string{"file", strconv.Itoa(i + 1)}, "")
266+
fieldName := f.FieldName
267+
268+
if fieldName == "" {
269+
if len(ro.Files) > 1 {
270+
fieldName = strings.Join([]string{"file", strconv.Itoa(i + 1)}, "")
271+
} else {
272+
fieldName = "file"
273+
}
270274
}
271275

272-
writer, err := multipartWriter.CreateFormFile(fileName, f.FileName)
276+
writer, err := multipartWriter.CreateFormFile(fieldName, f.FileName)
273277

274278
if err != nil {
275279
return nil, err

0 commit comments

Comments
 (0)