1

I am using the Echo framework for a post endpoint that accepts the form data. I am using a Struct as a binding model to extract the form data. my binding model and upload handler code looks like below.

 type FormModel struct {
    ID string                `form:"ID"`
    FirstName string                `form:"FirstName"`
    File      *multipart.FileHeader `form:"myFileName"`
}

func (cs *handler) uploadForm(c echo.Context) error {
s := new(FormModel)
if err := c.Bind(s); err != nil {
    return nil
}

fileHandler, err := c.FormFile("myFileName")

I am able to get the form Values like ID and FirstName with binding. But I am not able to get the file during binding. I have to use fileHandler, err := c.FormFile("myFileName") to get the file. is there any way to get the file Info with in the binding model?

4

1 回答 1

2

echo 默认不支持绑定 multipart.Form.File 数据,需要重新绑定实现接口。

为 echo Bind 封装了一层额外的 bind FormFile。如果结构指针类型的属性类型为*multipart.FileHeader 或[]*multipart.FileHeader,则该属性将通过反射设置为FormFile 的值。

我大概实现了这个功能。我没有使用过echo,也没有测试过,但想法是正确的。


var (
    typeMultipartFileHeader      = reflect.TypeOf((*multipart.FileHeader)(nil)).Elem()
    typeMultipartSliceFileHeader = reflect.TypeOf(([]*multipart.FileHeader)(nil)).Elem()
)

func main() {
    app := echo.New()
    // warp bind suppet bind FormFile
    app.Binder = NewBindFile(app.Binder)
}

type BindFunc func(interface{}, echo.Context) error

func (fn BindFunc) Bind(i interface{}, ctx echo.Context) error {
    return fn(i, ctx)
}

func NewBindFile(b echo.Binder) echo.Binder {
    return BindFunc(func(i interface{}, ctx echo.Context) error {
        err := b.Bind(i, ctx)
        if err == nil {
            ctype := crx.Request.Header.Get(echo.HeaderContentType)
            // if bind form
            if strings.HasPrefix(ctype, echo.MIMEApplicationForm) || strings.HasPrefix(ctype, echo.MIMEMultipartForm) {
                // get form files
                var form *multipart.Form
                form, err = ctx.MultipartForm()
                if err == nil {
                    err = EchoBindFile(i, ctx, form.File)
                }
            }
        }
        return err
    })
}

func EchoBindFile(i interface{}, ctx echo.Context, files map[string][]*FileHeader) error {
    iValue := reflect.Indirect(reflect.ValueOf(i))
    // check bind type is struct pointer
    if iValue.Kind() != reflect.Struct {
        return fmt.Errorf("BindFile input not is struct pointer, indirect type is %s", iValue.Type().String())
    }

    iType := iValue.Type()
    for i := 0; i < iType.NumField(); i++ {
        fType := iType.Field(i)
        // check canset field
        fValue := iValue.Field(i)
        if !fValue.CanSet() {
            continue
        }
        // revc type must *multipart.FileHeader or []*multipart.FileHeader
        switch fType.Type {
        case typeMultipartFileHeader:
            file := getFiles(files, fType.Name, fType.Tag.Get("form"))
            if file != nil && len(file) > 0 {
                fValue.Set(reflect.ValueOf(file[0]))
            }
        case typeMultipartSliceFileHeader:
            file := getFiles(files, fType.Name, fType.Tag.Get("form"))
            if file != nil && len(file) > 0 {
                fValue.Set(reflect.ValueOf(file))
            }
        }
    }
    return nil
}

func getFiles(files map[string][]*FileHeader, names ...string) []*FileHeader {
    for _, name := range names {
        file, ok := files[name]
        if ok {
            return file
        }
    }
    return nil
}
于 2020-05-20T16:19:46.597 回答