二开案例.WebApi.文件服务.附件上传(单据体)

栏目:云星空知识作者:金蝶来源:金蝶云社区发布:2024-09-23浏览:1

二开案例.WebApi.文件服务.附件上传(单据体)

【应用场景】

WebApi,单据体附件上传。



【案例演示】

采购订单,使用WebApi接口,给采购订单明细单据体上传附件。



【准备工作】

新建一张采购订单,如下图。



【实现步骤】

<1>学习附件上传接口。

学习资料:【WebAPI附件上传下载接口】https://vip.kingdee.com/article/296577252589190400

接口名称:AttachmentUpload

请求Url:

http://[IP]/K3Cloud/Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.AttachmentUpLoad.common.kdsvc

请求数据包:

{
    "FileName": "8.webp",
    "FormId": "PUR_PurchaseOrder",
    "IsLast": "true",
    "InterId": "107361",
    "BillNo": "CGDD001939",
    "EntryKey": "FPOOrderEntry",
    "EntryInterId": "109819",
    "AliasFileName": "8.webp",
    "FileId": "914ad20995d34178b34ec264b23f5f52",
    "SendByte": "iVBORw0KGgrkJggg=="
}


响应数据包:

{
    "Result": {
        "ResponseStatus": {
            "IsSuccess": true,
            "Errors": [],
                "SuccessEntitys": [{
                "Id": 692203,
                "Number": null,
                "DIndex": 0
            }],
            "SuccessMessages": [],
            "MsgCode": 0
        },
        "FileId": "914ad20995d34178b34ec264b23f5f52",
        "Message": ""
    }
}

注意:单据体附件上传,要比单据头附件上传,多传递两个参数(单据体标识和分录内码)。


<2>编写附件上传的WebApi接口调用代码。

using Kingdee.BOS.WebApi.Client;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using System;
using System.IO;
namespace Jac.XkDemo.BOS.WebApiTests
{
    /// <summary>
    /// WebApi.文件服务.附件上传(单据体)
    /// </summary>
    [TestClass]
    public class FileServiceEntryUploadTest
    {
        /// <summary>
        /// 附件上传(单据体)
        /// </summary>
        [TestMethod]
        public void UploadTest()
        {
            // 星空站点Url
            var webSite = "http://172.17.3.148/k3cloud/";
            // 数据中心ID(账套ID)
            var dbId = "6371de6bdb991c";
            // 登录账号          
            var userName = "demo";
            //第三方系统应用Id             
            var password = "888888";
            // 登录语言
            var lcId = 2052;
            // 登录
            var client = new K3CloudApiClient(webSite);
            var loginResult = client.Login(dbId, userName, password, lcId);
            if (!loginResult)
            {
                Assert.IsFalse(true);
                return;
            }
            // 给采购订单上传单据体附件
            var formId = "PUR_PurchaseOrder";
            var billNo = "CGDD001939";
            var id = "107361"; // SELECT * FROM T_PUR_POORDER WHERE FBILLNO='CGDD001939'
            var entryKey = "FPOOrderEntry";
            var entryId = "109819"; // SELECT a.* FROM T_PUR_POORDERENTRY a JOIN T_PUR_POORDER b ON a.FID=b.FID WHERE b.FBILLNO='CGDD001939' ORDER BY a.FSEQ
            var fileName = "8.webp";
            var filePath = @"C:\Users\Public\Pictures\Sample Pictures\8.webp";
            var fileBytes = File.ReadAllBytes(filePath);
            var fileBase64String = Convert.ToBase64String(fileBytes);
            /*
{
"FileName": "8.webp",
"FormId": "PUR_PurchaseOrder",
"IsLast": "true",
"InterId": "107361",
"BillNo": "CGDD001939",
"EntryKey": "FPOOrderEntry",
"EntryInterId": "109819",
"AliasFileName": "8.webp",
"FileId": "914ad20995d34178b34ec264b23f5f52",
"SendByte": "iVBORw0KGgrkJggg=="
}
             */
            var dataObj = new
            {
                FileName = fileName,
                FormId = formId,
                IsLast = "true",
                InterId = id,
                BillNo = billNo,
                EntryKey = entryKey,
                EntryInterId = entryId,
                AliasFileName = fileName,
                SendByte = fileBase64String
            };
            var data = JsonConvert.SerializeObject(dataObj);
            //Console.WriteLine(data);
            var result = client.AttachmentUpload(data);
            Console.WriteLine(result);
            var responseDto = ResponseDto.Parse(result);
            Assert.IsTrue(responseDto != null);
            Assert.IsTrue(responseDto.Result != null);
            Assert.IsTrue(responseDto.Result.ResponseStatus != null);
            Assert.IsTrue(responseDto.Result.ResponseStatus.IsSuccess);
            Assert.IsTrue(!string.IsNullOrWhiteSpace(responseDto.Result.FileId));
            Console.WriteLine(responseDto.Result.FileId);
        }
        #region ResponseDto
        /// <summary>
        /// 响应数据包
        /// </summary>
        public class ResponseDto
        {
            #region method
            /// <summary>
            /// 将当前对象序列化为Json字符串
            /// </summary>
            /// <returns></returns>
            public virtual string ToJson()
            {
                return JsonConvert.SerializeObject(this);
            }
            /// <summary>
            /// 将Json字符串反序列化为指定对象
            /// </summary>
            /// <param name="json"></param>
            /// <returns></returns>
            public static ResponseDto Parse(string json)
            {
                return JsonConvert.DeserializeObject<ResponseDto>(json);
            }
            #endregion
            #region property
            /// <summary>
            /// 响应结果
            /// </summary>
            public ResponseResult Result { get; set; }
            #endregion
            #region class
            public class ResponseResult
            {
                public string Id { get; set; }
                public string FileId { get; set; }
                public ResponseResultStatus ResponseStatus { get; set; }
                public class ResponseResultStatus
                {
                    public string MsgCode { get; set; }
                    public bool IsSuccess { get; set; }
                    public string ErrorCode { get; set; }
                    public System.Collections.Generic.IList<ResponseMessage> Errors { get; set; }
                    public System.Collections.Generic.IList<ResponseMessage> SuccessMessages { get; set; }
                    public System.Collections.Generic.IList<SuccessEntity> SuccessEntitys { get; set; }
                    public class ResponseMessage
                    {
                        public string FieldName { get; set; }
                        public string Message { get; set; }
                        public int DIndex { get; set; }
                    }
                    public class SuccessEntity
                    {
                        public string Id { get; set; }
                        public string Number { get; set; }
                        public string BillNo { get; set; }
                        public int DIndex { get; set; }
                    }
                }
                #endregion
            }
            #endregion
        }
    }
}



【功能验证】

执行单元测试,即可上传采购订单单据体附件,可通过明细信息单据体的附件菜单查询已上传的附件,也可以通过SQL查询附件信息。

查询SQL:

-- 查询采购订单
SELECT * FROM T_PUR_POORDER WHERE FBILLNO='CGDD001939'
-- 查询采购订单明细
SELECT a.* FROM T_PUR_POORDERENTRY a JOIN T_PUR_POORDER b ON a.FID=b.FID WHERE b.FBILLNO='CGDD001939' ORDER BY a.FSEQ
-- 查询采购订单的单据头附件
SELECT * FROM T_BAS_ATTACHMENT WHERE FBILLTYPE='PUR_PurchaseOrder' AND FINTERID='107361' AND FENTRYKEY='FPOOrderEntry' AND FENTRYINTERID='109819'
ORDER BY FCREATETIME

---------------------------------------------------------------------------------------------------------













【金蝶云星空BOS二次开发案例演示】https://vip.kingdee.com/article/94751030918525696

二开案例.WebApi.文件服务.附件上传(单据体)

【应用场景】WebApi,单据体附件上传。【案例演示】采购订单,使用WebApi接口,给采购订单明细单据体上传附件。【准备工作】新建一张采购订...
点击下载文档
确认删除?
回到顶部
客服QQ
  • 客服QQ点击这里给我发消息