知识分享 - 如何上传并存储图片文件到数据库
金蝶云社区-JohnnyDing
JohnnyDing
4人赞赏了该文章 7,180次浏览 未经作者许可,禁止转载编辑于2016年02月25日 15:45:20
summary-icon摘要由AI智能服务提供

本文介绍了一个技术案例,旨在解决将图片文件上传并存储到数据库的问题。通过自定义单据,添加了文件上传控件和图片展示字段,并设置了两个按钮分别用于将上传的图片填充到图片字段和将图片数据保存到数据库。在插件代码中,通过事件监听处理文件上传后的操作,使用Binary类型成功将图片数据插入数据库表格,并在界面上展示了上传和存储后的效果。

需求背景:
需要自行把各种来源的图片文件,存储到数据库物理表格。

技术难点:
1. 如何上传文件?
2. 如何把上传的文件存入数据库?

案例设计:
1. 自定义一个单据,添加一个文件上传控件(F_JD_FileUpdate),用来上传图片;
2. 添加一个图片字段(F_JD_Picture),用于展示已经存储到数据库的图片,以测试效果;
3. 添加一个按钮:设置字段值(F_JD_BTNSetFileFieldValue),用来把上传的图片文件,填写到图片字段中。保存单据时,即会自动保存图片内容;
4. 添加一个按钮:存储到数据库(F_JD_BTNSaveToDB),用来向本单的表格,插入一条新记录,存储上传的图片文件;刷新单据列表可以看到新增的记录;

界面效果
1. 界面原始效果:


2. 上传图片后,设置图片字段值之后的效果:

插件代码:
//*****************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Data;
using System.IO;


using Kingdee.BOS;
using Kingdee.BOS.Util;
using Kingdee.BOS.JSON;
using Kingdee.BOS.Core;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.Bill.PlugIn.Args;
using Kingdee.BOS.ServiceHelper;


namespace JDSample.FormPlugIn.Bill
{
///


/// 演示上传文件并保存到数据库
///

[Description("演示上传文件并保存到数据库")]
public class S20160224UpdateFileEdit : AbstractBillPlugIn
{
///
/// 上传上来的文件名:完整的文件名,包含了物理路径
///

private string _fullFileName = string.Empty;

public override void AfterBindData(EventArgs e)
{
// 设置"保存"按钮,在界面刚加载时,灰显
this.View.GetControl("F_JD_BTNSaveToDB").Enabled = false;
this.View.GetControl("F_JD_BTNSetFileFieldValue").Enabled = false;
}

///


/// 文件上传完毕,触发此事件:通过此事件获取上传上来的文件名
///

///
public override void CustomEvents(CustomEventsArgs e)
{
if (e.Key.EqualsIgnoreCase("F_JD_FileUpdate"))
{
this.View.GetControl("F_JD_FileUpdate").SetCustomPropertyValue("NeedCallback", true);
this.View.GetControl("F_JD_FileUpdate").SetCustomPropertyValue("IsRequesting", false);

if (e.EventName.EqualsIgnoreCase("FileChanged"))
{// 文件上传完毕


// 取文件上传参数,文件名
JSONObject uploadInfo = KDObjectConverter.DeserializeObject(e.EventArgs);
if (uploadInfo != null)
{
JSONArray json = new JSONArray(uploadInfo["NewValue"].ToString());
if (json.Count > 0)
{
// 取上传的文件名
string fileName = (json[0] as Dictionary)["ServerFileName"].ToString();
this._fullFileName = this.GetFullFileName(fileName);

// 解锁按钮
this.View.GetControl("F_JD_BTNSaveToDB").Enabled = true;
this.View.GetControl("F_JD_BTNSetFileFieldValue").Enabled = true;
}
else
{
// 锁定按钮
this.View.GetControl("F_JD_BTNSaveToDB").Enabled = false;
this.View.GetControl("F_JD_BTNSetFileFieldValue").Enabled = false;

}
}
}
}
}

///


/// 按钮点击事件:保存图片
///

///
public override void ButtonClick(ButtonClickEventArgs e)
{
if (e.Key.EqualsIgnoreCase("F_JD_BTNSaveToDB"))
{// 直接保存图片文件到数据库
this.SaveFileToDB();
}
else if (e.Key.EqualsIgnoreCase("F_JD_BTNSetFileFieldValue"))
{// 把图片内容,填充到界面上的图片字段中
this.SetFileFieldValue();
}
}

///
/// 把图片数据,存入到单据数据包 - 图片字段中
///

private void SetFileFieldValue()
{
Byte[] fileValue = this.ReadFile(this._fullFileName);
this.Model.SetValue("F_JD_Picture", fileValue);
}

///


/// 把图片数据,直接存入到数据库表格中
///

private void SaveFileToDB()
{
// 获取表格名称
string tableName = this.View.BillBusinessInfo.Entrys[0].TableName;
// 表格主键字段名
string pkFieldName = this.View.BillBusinessInfo.GetForm().PkFieldName;
// 图片字段名(Image类型)
string imageFieldName = "F_JD_Picture";

// SQL语句
string sql = string.Format("INSERT INTO {0} ({1}, {2}, {3}) Values (@FID, @FBillNo, @Image)",
tableName,
pkFieldName,
this.View.BillBusinessInfo.GetBillNoField().FieldName,
imageFieldName);

// 准备字段值:
// 主键值
var ids = DBServiceHelper.GetSequenceInt64(this.Context,
tableName,
1);
// 图片内容
Byte[] fileValue = this.ReadFile(this._fullFileName);

// 准备SQL参数
List paramList = new List();
paramList.Add(new SqlParam("@FID", KDDbType.Int64, ids.ElementAt(0)));
paramList.Add(new SqlParam("@FBillNo", KDDbType.String, "JD" + Convert.ToString(ids.ElementAt(0))));

// 图片字段的参数类型
// 使用KDDbType.String类型,会报类型转换失败
//paramList.Add(new SqlParam("@Image", KDDbType.String, fileValue));
// 使用KDDbType.Object类型,会报超长
//paramList.Add(new SqlParam("@Image", KDDbType.Object, fileValue));
// 使用KDDbType.Binary类型可行,测试图片能够存入(测试图片2,280K)
paramList.Add(new SqlParam("@Image", KDDbType.Binary, fileValue));


DBServiceHelper.Execute(this.Context, sql, paramList);
}


///


/// 产生完整的文件名,包含了物理路径
///

///
///
private string GetFullFileName(string fileName)
{
string dir = "FileUpLoadServices\\UploadFiles";
return PathUtils.GetPhysicalPath(dir, fileName);
}

///
/// 读取图片文件
///

///
///
private Byte[] ReadFile(string fileName)
{
if (string.IsNullOrWhiteSpace(fileName))
{
return null;
}
else
{
using (FileStream fs = new FileStream(this._fullFileName, FileMode.Open, FileAccess.Read))
{
BinaryReader br = new BinaryReader(fs);
return br.ReadBytes(Convert.ToInt32(fs.Length));
}
}
}
}
}

您的鼓励与嘉奖将成为创作者们前进的动力,如果觉得本文还不错,可以给予作者创作打赏哦!

请选择打赏金币数 *

10金币20金币30金币40金币50金币60金币
可用金币: 0