【知识分享】移动附件下载
金蝶云社区-用户已注销
用户已注销
2人赞赏了该文章 6216次浏览 未经作者许可,禁止转载编辑于2016年11月29日 11:03:00

一、前言
1. 为了明晰上传与下载功能的讲解,这里将分两篇文章来分别讲解上传与下载功能。
2. 本篇将完整讲解如何实现移动端的附件下载功能:
2.1 从数据库下载附件
2.2 从文件服务器下载附件
3. 相关链接详见文章末尾。

二、移动附件下载开发教程
附件下载涉及以下几个步骤:
从数据库查询关联附件 => 下载至本地临时目录 => 使用控件进行展示

以下将进行逐步构建和讲解:
从新建一张表单开始
1. 登陆BOS IDE
2. 打开子系统
3. 新建移动表单


再添加一些控件
1. 添加按钮(通用控件),用于点击时下载附件


2. 添加多行文本,用于记录下载日志


3. 添加附件字段,用于展示下载后的附件


我们想要的功能,通过插件实现
1. 这里附件下载逻辑涉及到以下几个点:
1.1 继《移动附件入门_上传篇》,我们采用BillTypeInterId组合来确定一张单据,并以此作为附件关联标识。
1.2 获取本地临时目录,用于保存下载下来的附件

[code]// 获取文件上传的临时目录
string tempDirPath = HttpContext.Current.Request.PhysicalApplicationPath + KeyConst.TEMPFILEPATH;[/code]1.3 如果附件是上传到文件服务器的,下载时还需要判断是否启用了文件服务器
[code]// 判断是否启用文件服务器
if (Kingdee.BOS.ServiceHelper.FileServer.FileServerHelper.UsedFileServer(this.Context))
{
// TODO:从文件服务器下载附件
}[/code]1.4 最后通过this.Model.SetValue方法将数据绑定到控件上
2. 以下是完整的插件代码,供大家参考。
[code]using Kingdee.BOS.Core;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.FileServer.Core.Object;
using Kingdee.BOS.FileServer.ProxyService;
using Kingdee.BOS.KDThread;
using Kingdee.BOS.Mobile.Metadata.ControlDataEntity;
using Kingdee.BOS.Mobile.PlugIn;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;

namespace Kingdee.BOS.Mobile.FormPlugIns.Test
{
///


/// 附件下载测试
///

///
/// 附件下载涉及以下步骤:
/// 1. 从数据库中读取关联标识的所有附件数据
/// 2. 根据FileStorage标识分别从数据库或文件服务器下载附件
/// 3. 将下载下来的附件保存到本地临时目录
///

[Description("测试附件下载")]
public class TestAccessoryDownload : AbstractMobilePlugin
{
public override void ButtonClick(Core.DynamicForm.PlugIn.Args.ButtonClickEventArgs e)
{
if ("FDownload".EqualsIgnoreCase(e.Key))
{
// 获取文件上传的临时目录
string tempDirPath = HttpContext.Current.Request.PhysicalApplicationPath + KeyConst.TEMPFILEPATH;

// 结合《移动附件入门_上传篇》中的虚拟FromID和虚拟内码结合来确定是哪一张单据
string billType = "Test_MOB_Accessory";// 关联单据的FormID
string interID = "D00001";// 关联单据/基础资料ID

// 加载数据库中附件数据
var filter = OQLFilter.CreateHeadEntityFilter(string.Format("FBillType = '{0}' AND FInterID = '{1}'", billType, interID));
var dyns = BusinessDataServiceHelper.Load(this.Context, FormIdConst.BOS_Attachment, null, filter);

List fileList = new List();
StringBuilder sb = new StringBuilder();
foreach (var dyn in dyns)
{
byte[] dataBuff = null;
if (Convert.ToInt16(dyn["FileStorage"]) != 0)
{
// 判断是否启用文件服务器
if (Kingdee.BOS.ServiceHelper.FileServer.FileServerHelper.UsedFileServer(this.Context))
{
// 采用文件服务器存储
var service = new UpDownloadService();
TFileInfo tFile = new TFileInfo()
{
FileId = dyn["FileId"].ToString(),
CTX = this.Context
};
dataBuff = service.GetFileData(tFile);
}
}
else
{
// 不是采用文件服务器存储,则尝试直接取数据
if (dyn["Attachment"] != null)
{
dataBuff = (byte[])dyn["Attachment"];
}
}

if (dataBuff == null)
{
sb.AppendLine("未能下载附件:" + dyn["AttachmentName"]);
continue;
}

var tempFileName = string.Format("{0}_{1}_{2}", this.Context.DBId, dyn["Id"], dyn["AttachmentName"]);
// 拼接文件名
string fullName = System.IO.Path.Combine(tempDirPath, tempFileName);
// 将附件保存至临时文件夹
System.IO.File.WriteAllBytes(fullName, dataBuff);
fileList.Add(new File() { Name = tempFileName, Type = dyn["ExtName"].ToString() });
}

AccessoryData data = new AccessoryData()
{
FormId = this.View.BusinessInfo.GetForm().Id,
BillId = "Test001",
Data = fileList
};

this.Model.SetValue("FAccessoryField", data.ToJsonString());
if (fileList.Count > 0)
{
sb.AppendLine();
sb.AppendLine(string.Join(",", fileList.Select(f => f.Name)) + ",下载成功。");
}
this.Model.SetValue("FLog", sb.ToString());
}

base.ButtonClick(e);
}
}
}
[/code]

三、移动附件下载开发过程中可能遇到的问题的QA部分
1. 待更新...

四、相关链接
1.【知识分享】移动附件_上传篇
2.【分享】K3 Cloud文件服务器配置指南
3.【分享】K3 Cloud移动产品相关文档下载(3g.k3cloud.kingdee.com/m/doc_dowload.htm)