Comments (5)
你这里的abc字段是unexported字段,reflect没法给它设值的。你把它改成Abc再试试
from admin.
谢谢你的回答。
可能是我举例不够注意,这里abc实际中已是大写。现象依旧。
from admin.
看你描述是reflectutils.Set
有问题,但我试了下并没有问题 https://go.dev/play/p/9NYvCIlc8PC 。你能给下完整的代码吗
from admin.
// 文章 type Document struct { gorm.Model
list:"-" edit:"-"Categorize DocumentCategorize
list:"-" edit:"-"// 分类 CategorizeId uint
list:"-" edit:"-"// Title string
name:"标题" // 文章标题 PicUrl string
list:"-"Pic PicFile
list:"-" name:"图片" // 自定义类型之后的,使用 reflectutils.Set 修改其值无效 Author string
name:"作者" list:"-" // 作者 Source string
name:"来源" list:"-" // 来源 Keyword string
name:"关键词" // 关键词 Content string
name:"内容" list:"-" // 内容 OrderBy uint
name:"排序" list:"-" // 排序 UserCategorize UserCategorize
list:"-" edit:"-" // 用户分类 UserCategorizeId uint
list:"-" edit:"-"` //
}
`
`
b.FieldDefaults(presets.WRITE).FieldType(PicFile("")).
ComponentFunc(func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) h.HTMLComponent {
//val := field.Value(obj).(PicFile)
doc := obj.(*Document)
val := doc.PicUrl
var img []h.HTMLComponent
if len(string(val)) > 0 { // 有内容则显示为img
for _, n := range strings.Split(string(val), ",") { // 可以保存多个图片
imgCode := h.Img(sysSet["DocUpUrl"]+n).
Attr("onclick", "javascript:removePic(this)").
Attr("title", "点击可删除")
img = append(img, imgCode)
}
}
var er h.HTMLComponent
if len(field.Errors) > 0 {
er = h.Div().Text(field.Errors[0]).Style("color:red")
}
return h.Div(
h.Div(img...).Class("pic"),
er, // 存在错误时才显示错误信息
h.Div(h.Input("").Type("file").Attr(web.VFieldName(fmt.Sprintf("%s_NewFile", field.Name))...)),
).Style("margin-bottom:20px")
}).
SetterFunc(func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) (err error) {
user := login.GetCurrentUser(ctx.R).(*User) // 获取用户ID
ff, fh, err := ctx.R.FormFile(fmt.Sprintf("%s_NewFile", field.Name)) // ff为上传文件编码 fh包含文件名及类型
if ff == nil || err != nil {
// err = reflectutils.Set(obj, field.Name, PicFile(doc.PicUrl))
return nil
}
// fh.Header["Content-Type"] 文件类型, fh.Filename 文件名
req, err := http.NewRequest("POST", sysSet["Url_DocPic"], ff) // 发送文件内容
if err != nil {
return
}
req.Header.Add("Filename", fh.Filename) // 通过header传输文件名
req.Header.Add("UserID", fmt.Sprintf("%d", user.ID)) // 通过header传输用户ID , 注意:接收的Header为首字母大写,其它均为小写
var res *http.Response
res, err = http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
// 获取返回信息
b, err := io.ReadAll(res.Body)
if err != nil {
return err
}
if res.StatusCode == 500 {
err = fmt.Errorf("%s", string(b))
return err
}
//err = reflectutils.Set(obj, field.Name, PicFile(b))
if PicFile(b) != "" {
doc := obj.(*Document)
if doc.PicUrl == "" {
err = reflectutils.Set(obj, "PicUrl", string(b))
} else {
err = reflectutils.Set(obj, "PicUrl", fmt.Sprintf("%s,%s", doc.PicUrl, string(b)))
}
}
return nil
})`
from admin.
@soease 你这贴的完全看不了
from admin.
Related Issues (11)
- Sorting tables HOT 1
- Custom find handler like qor4 HOT 3
- custom a html page for a menu HOT 2
- How to display fields based on different permissions? HOT 2
- Field Except() function is not working. HOT 3
- If the class is set to Select, the value cannot be obtained when updating! help HOT 2
- create multiple file field HOT 1
- Remove usages of unlicensed code from reflectutils HOT 1
- panic: interface conversion: interface {} is **activity.ActivityLog, not *activity.ActivityLog
- example :presets:user_management:users:1: represents the user record with id 1 under uri user_management. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from admin.