using Kingdee.BOS.Core;
using Kingdee.BOS.Core.Metadata;
using kingdee.Bos.file服务器. core;
using kingdee.Bos.file server.core.object;
using kingdee.Bos.file server.proxy service;
using Kingdee.BOS.KDThread;
using kingdee.Bos.mobile.metadata.control data entity;
using Kingdee.BOS.Mobile.PlugIn;
using kingdee.Bos.mobile.plugin.control model;
using kingdee.Bos.ORM.data entity;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Util;
用户系统;
using system.collections.generic;
using系统.component model;
using System.Linq;
using System.Text;
using系统. web;
namespace kingdee.Bos.mobile.form plugins.test
{
///
//附件上传测试
///
///
//附件上传包括以下步骤。
//1 .附件控件将选定的文件复制到临时目录
//2 .触发aftermobileupload函数并传递所需的参数
//3 .将文件从临时目录保存到数据库或文件服务器
///
说明(“测试附件上载”)
publicclasstestaccessoryupload : abstractmobileplugin
{
///
//上传类型,0为数据库,1为文件服务,2为亚马逊云,3为金蝶云
///
私密int submit type=0;
///
//按钮单击事件
//
//
publicoverridevoidbuttonclick (core.dynamic form.plugin.args.buttonclickeventargse ) )。
{
if(fsubmit2db ).equalsignorecase(e.key ) ) ) ) ) )。
{
//使用数据库访问附件
提交类型=0;
}
else if (f submit 2文件服务器(.equalsignorecase ) (e.key ) ) )。
{
//以文件服务器方式访问附件
//检查文件服务器是否有效
if (! kingdee.Bos.service helper.file server.file server helper.used file server (this.context ) )
{
this.View.ShowMessage ('文件服务器未启用,无法上载。' );
返回;
}
使用Cloud服务器配置的存储方法保存附件
submit type=kingdee.Bos.service helper.file server.file server helper.getfilestorgaetype (this.context );
}
//首先将客户端附件上传到临时目录
this.view.get control (ffile update ) ).UploadFieldBatch );
base.Buttonclick(e;
}
//
//将附件上传到本地临时目录后的回调函数
//
//
publicoverridevoidaftermobileupload (plugin.args.mobileuploadeventargse ) )。
{
//获取服务器的临时目录
string tempdir path=httpcontext.current.request.physicalapplicationpathkeyconst.tempfile path;
//获取附件表的元数据类
var formMetadata = (FormMetadata)MetaDataServiceHelper.Load(this.Context, FormIdConst.BOS_Attachment);List dynList = new List();
StringBuilder sb = new StringBuilder();
foreach (FiledUploadEntity file in e.FileNameArray)
{
// 检查文件是否成功上传到临时目录
if (!file.IsSuccess)
{
continue;
}
// 检查文件是否存在于临时目录
var fileName = System.IO.Path.Combine(tempDirPath, file.FileName);
if (!System.IO.File.Exists(fileName))
{
continue;
}
/**
* 此处几个关键属性解读:
* 1. BillType 关联的模型的FormId
* 2. BillNo 关联的单据编号,用于确定此附件是属于哪张单据
* 3. InterID 关联的单据/基础资料ID,附件列表就是根据这个ID进行加载
* 4. EntryInterID 关联的单据体ID,这里我们只演示单据头,所以固定设置为-1
* 5. AttachmentSize 系统统一按照KB单位进行显示,所以需要除以1024
* 6. FileStorage 文件存储类型,0为数据库,1为文件服务,2为亚马逊云,3为金蝶云
*/
var dataBuff = System.IO.File.ReadAllBytes(fileName);
var dyn = new DynamicObject(formMetadata.BusinessInfo.GetDynamicObjectType());
if (submitType != 0)
{
// 将数据上传至文件服务器,并返回上传结果
var result = this.UploadAttachment(file.FileName, dataBuff);
if (!result.Success)
{
// 上传失败,收集失败信息
sb.AppendLine(string.Format("附件:{0},上传失败:{1}", file.FileName, result.Message));
continue;
}
// 通过这个FileId就可以从文件服务器下载到对应的附件
dyn["FileId"] = result.FileId;
}
else
{
// 数据库的方式是直接保存附件数据
dyn["Attachment"] = dataBuff;
}
// 此处我们不绑定到特定的单据,为了简化示例,只实现单纯的文件上传与下载
dyn["BillType"] = "Test_MOB_Accessory"; // 虚拟FormId
dyn["BillNo"] = "A00001"; // 虚拟的单据编号
dyn["InterID"] = "D00001"; // 虚拟的InterId,这个ID将作为我们下载的识别标识
// 上传文件服务器成功后才加入列表
dyn["AttachmentName"] = file.FileName;
dyn["AttachmentSize"] = Math.Round(dataBuff.Length / 1024.0, 2);
dyn["EntryInterID"] = -1;// 参照属性解读
dyn["CreateMen_Id"] = Convert.ToInt32(this.Context.UserId);
dyn["CreateMen"] = GetUser(this.Context.UserId.ToString());
dyn["ModifyTime"] = dyn["CreateTime"] = TimeServiceHelper.GetSystemDateTime(this.Context);
dyn["ExtName"] = System.IO.Path.GetExtension(file.FileName);
dyn["FileStorage"] = submitType.ToString();
dynList.Add(dyn);
}
if (dynList.Count > 0)
{
// 所有数据加载完成后再一次性保存全部
BusinessDataServiceHelper.Save(this.Context, dynList.ToArray());
sb.AppendLine();
sb.AppendLine(string.Join(",", dynList.Select(dyn => dyn["AttachmentName"].ToString()).ToArray()) + ",上传成功");
}
this.Model.SetValue("FLog", sb.ToString());
base.AfterMobileUpload(e);
}
///
/// 上传附件方法
///
///
///
///
private FileUploadResult UploadAttachment(string fileName, byte[] dataBuff)
{
// 初始化上传下载服务,这个Service会根据Cloud配置自动上传到对应的文件服务器
var service = new UpDownloadService();
int len = 0, less = 0;
string fileId = null;
byte[] buff = null;
while (len < dataBuff.Length)
{
// 文件服务器采用分段上传,每次上传4096字节, 最后一次如果不够则上传剩余长度
less = (dataBuff.Length - len) >= 4096 ? 4096 : (dataBuff.Length - len);
buff = new byte[less];
Array.Copy(dataBuff, len, buff, 0, less);
len += less;
using (System.IO.MemoryStream ms = new System.IO.MemoryStream(buff))
{
TFileInfo tFile = new TFileInfo()
{
FileId = fileId,
FileName = fileName,
CTX = this.Context,
Last = len >= dataBuff.Length,//标记是否为文件的最后一个片段
Stream = ms
};
var result = service.UploadAttachment(tFile);
// 注意点:上传时fileId传入null为新文件开始,会返回一个文件的fileId,后续采用这个fileId标识均为同一文件的不同片段。
fileId = result.FileId;
if (!result.Success)
{
return result;
}
}
}
return new FileUploadResult()
{
Success = true,
FileId = fileId
};
}
///
/// 获取用户
///
///
///
DynamicObject GetUser(string userID)
{
OQLFilter filter = OQLFilter.CreateHeadEntityFilter(string.Format("FUSERID={0}", userID));
return BusinessDataServiceHelper.Load(this.View.Context, FormIdConst.SEC_User, null, filter).FirstOrDefault();
}
}
}