String ls_path, ls_file_name, ls_filter, ls_err
Int li_ret, li_loop, li_i, li_file, li_bytes
Long ll_file_len
Blob lb_file, lb_read, lb_pic
// 选择图片文件
ls_filter = "jpg File (*.jpg),*.jpg,bmp file(*.bmp),*.bmp,gif file(*.gif),*.gif"
li_ret = GetFileOpenName("选择头像", ls_path, ls_file_name, "png", ls_filter)
If Isnull(li_ret) Or li_ret < 0 Then
Messagebox("提示", "打开文件错误!")
Return
End If
// 读取文件二进制数据
ll_file_len = FileLength(ls_path)
// 5KB
If ll_file_len > 5120 Then
Messagebox("提示", "文件大小不能超过5KB!")
Return
End If
// FileRead每次最多读取32765bytes
If ll_file_len > 32765 Then
li_loop = (ll_file_len / 32755) + 1
Else
li_loop = 1
End If
li_file = FileOpen(ls_path, StreamMode!, Read!, LockRead!)
For li_i = 1 To li_loop
li_bytes = FileRead(li_file, lb_read)
If Isnull(li_bytes) Then
Messagebox("提示", "读取文件错误!")
Return
End If
If li_bytes > 0 Then
lb_file = lb_file + lb_read
End If
Next
FileClose(li_file)
// 设置图片
p_avatar.setpicture(lb_file)
// 保存图片
li_i = 0
SELECT COUNT(*) INTO :li_i FROM T_SYS_USER_INFO WHERE USER_GH = :gs_operid;
If li_i > 0 Then
UPDATEBLOB T_USER SET AVATAR_PIC = :lb_file WHERE USER_GH = :gs_operid;
If Sqlca.Sqlcode <> 0 Then
ls_err = Sqlca.Sqlerrtext
ROLLBACK;
Messagebox("提示", "头像保存失败:" + ls_err)
Return
End If
ls_file_name = gs_operid + '_' + String(pub_fc_getdate(), "yyyy-mm-dd")
UPDATE T_USER SET AVATAR_NAME = :ls_file_name WHERE USER_GH = :gs_operid;
If Sqlca.Sqlcode <> 0 Then
ls_err = Sqlca.Sqlerrtext
ROLLBACK;
Messagebox("提示", "头像名称保存失败:" + ls_err)
Return
End If
End If
COMMIT;
// 从数据库读取图片
li_i = 0
SELECT COUNT(*) INTO :li_i FROM T_SYS_USER_INFO WHERE USER_GH = :gs_operid;
If li_i > 0 Then
SELECTBLOB AVATAR_PIC INTO :lb_pic FROM T_USER WHERE USER_GH = :gs_operid;
p_1.setpicture(lb_pic)
End If
// 图片较小 sqlserver 可用 varbinary(字段长度)类型来保存