二开插件:pc单据附件和图片字段,在移动端显示和下载原创
金蝶云社区-暗夜
暗夜
4人赞赏了该文章 1391次浏览 未经作者许可,禁止转载编辑于2022年12月19日 09:37:05

       经常收到小伙伴的反馈,pc单据附件和图片字段,怎么在移动表单中显示和下载呢?今天通过一个插件示例,来完成该工作;
      适用范围:pc端对应的附件文件服务器,附件数据库,图片文件服务器,图片数据库等控件,与移动端的附件控件、图片控件相关联
      温馨提示:如果需要在移动端,显示的是pc端单据的附件列表中的附件,那么直接使用单据附件控件即可;
      准备工作:如果仅显示pc端的附件文件服务器,附件数据库,那么7.5以上版本均可;如果需要显示pc端的图片文件服务器,图片数据库,则需要升级2021年3月份及以上的补丁;
      需求描述(示例):测试单据中包含:附件文件服务器,附件数据库,图片文件服务器,图片数据库4个字段,要求在移动表单中显示和下载,数据互通; 测试单据示例:  

QQ截图20210930181531.png

 

         移动表单设计:如下图所示,包含附件控件和图片控件:
image.png 
    
        移动端运行效果图:

     QQ截图20210930181505.png

     

      插件代码:

       public override void AfterBindData(EventArgs e)
        {
            base.AfterBindData(e);
            BindValue();
        }
        private void BindValue()
        {
            string _FormId = "kd_FlexTest"; // 目标单据唯一标识,即formid
            string _InterID = "100016"; // 单据内码,这个ID将作为我们下载的识别标识
            //string controlKey = "F_MOB_FileServer"; //单据中文件服务器控件的字段名
            //string controlKey = "F_MOB_FileDatabase"; //单据中文件数据库控件的字段名
            //string controlKey = "F_MOB_ImageServer"; //单据中图片服务器控件的字段名
            string controlKey = "F_MOB_ImageDatabase"; //单据中图片数据库控件的字段名
            var businessInfo = ((FormMetadata)MetaDataServiceHelper.Load(this.Context, _FormId)).BusinessInfo;
            var dyn = BusinessDataServiceHelper.LoadSingle(this.Context, _InterID, businessInfo.GetDynamicObjectType());
            if (dyn == null) return;
            var dynFile = dyn[controlKey];
            if (dynFile == null) return;
            List<File> files = new List<File>();
            string path = HttpContext.Current.Request.PhysicalApplicationPath + KeyConst.TEMPFILEPATH; // 获取文件上传的临时目录
                      if (controlKey == "F_MOB_FileDatabase") //pc单据对应的控件类型为文件数据库
            {
                string fieldValue = dynFile.ToString(); //文件数据库,实际存储的是转成base64字符串的json数组
                JSONArray fileArray = fieldValue.IsEmpty() ? new JSONArray() : SerializatonUtil.DeserializeFromBase64<JSONArray>(fieldValue);
                foreach (JSONObject json in fileArray)
                {
                    string fileId = string.Empty;
                    string serverFileName = json.GetString("ServerFileName");
                    string fileName = json.GetString("FileName");
                    string fileType = fileName.Substring(fileName.LastIndexOf(".") + 1);
                    
                    //文件数据库储存方式,根据byte数据组保存文件到临时目录,返回文件名给前端,前端根据文件名获取数据                    
                    int length = serverFileName.IndexOf(" ") > -1 ? serverFileName.IndexOf(" ") : serverFileName.Length;
                    fileId = serverFileName.Substring(0, length); //附件(数据库),serverFileName为fileid+空格+文件名+后缀,需要截取 
                    fileName = CommonFunctionUtil.FilterOpSysInvalidCharacterForFileName(fileName);
                    fileName = CommonFunctionUtil.GetFileName(this.Context, fileId, fileName);
                    string filePath = System.IO.Path.Combine(path, fileName);
                    //输出原始文件
                    if (filePath == "" || !System.IO.File.Exists(filePath))
                    {
                        byte[] fileBytes = json.GetValue<Byte[]>("FileContent");
                        if (fileBytes != null)
                        {
                            CreateFile(fileBytes, filePath);
                        }
                    }
                    files.Add(new File() { FileID = fileId, Name = fileName, Type = fileType, IsFieldFile = true });
                }
            }
            else if (controlKey == "F_MOB_FileServer" || controlKey == "F_MOB_ImageServer") //pc单据对应的控件类型为文件服务器或图片文件服务器
            {
                string fileId = dynFile.ToString(); //文件服务器和图片文件服务器,实际存储的是文件服务器中的fileid
                var file = FileServerHelper.GetKDFileById(this.Context, fileId);
                if (file == null) return;
                string fileName = file.Name;
                string fileType = file.suffix;
                files.Add(new File() { FileID = fileId, Name = fileName, Type = fileType, IsFieldFile = true });
            }
            else if (controlKey == "F_MOB_ImageDatabase") //pc单据对应的控件类型为图片数据库库
            {
                string fileId = string.Empty; //图片数据库,实际存储的是图片的byte数组
                string fileType = ".jpg";
                Random random = new Random();
                string fileName = string.Format("{0}{1}{2}{3}{4}", DateTime.Now.ToString("yyyyMMddHHmmssfff"), this.Context.UserId, 
System.Threading.Thread.CurrentThread.ManagedThreadId, random.Next(int.MaxValue), fileType);
                string filePath = System.IO.Path.Combine(path, fileName);
                //输出原始文件
                if (filePath == "" || !System.IO.File.Exists(filePath))
                {
                    byte[] fileBytes = (byte[])dynFile;
                    if (fileBytes != null)
                    {
                        CreateFile(fileBytes, filePath);
                    }
                }
                files.Add(new File() { FileID = fileId, Name = fileName, Type = fileType, IsFieldFile = true });
                       }
            AccessoryData data = new AccessoryData()
            {
                FormId = _FormId,
                BillId = _InterID,
                Data = files
            };
            if (controlKey == "F_MOB_FileDatabase" || controlKey == "F_MOB_FileServer")
                 {
                 this.View.GetControl("FFileUpload").SetValue(data.ToJsonString());
            }
            if (controlKey == "F_MOB_ImageDatabase" || controlKey == "F_MOB_ImageServer")
            {
                this.View.GetControl("FImageUpload").SetValue(data.ToJsonString());
            }
        }
        
        /// <summary>
        /// 根据二进制文件,创建临时文件
        /// </summary>
        private void CreateFile(byte[] fileBytes, string filePath)
        {
            try
            {
                if (!System.IO.File.Exists(filePath) && fileBytes != null && fileBytes.Length > 0)
                {
                    using (System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.Read))
                    using (System.IO.BinaryWriter bw = new System.IO.BinaryWriter(fs))
                    {
                        bw.Write(fileBytes);
                    }
                }
            }
            catch { }
        }





赞 4