二开案例.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", "AliasFileName": "8.webp", "FileId": "914ad20995d34178b34ec264b23f5f52", "SendByte": "iVBORw0KGgrkJggg==" }
响应数据包:
{ "Result": { "ResponseStatus": { "IsSuccess": true, "Errors": [], "SuccessEntitys": [{ "Id": 692162, "Number": null, "DIndex": 0 }], "SuccessMessages": [], "MsgCode": 0 }, "FileId": "1ce11300f61444ef929f6d2edf281f31", "Message": "" } }
<2>编写附件上传的WebApi接口调用代码,本案例提供了附件上传(小于6M的文件)和附件分块上传两种(大于6M的文件)使用场景,如下。
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 FileServiceUploadTest { /// <summary> /// 附件上传(小于6M的文件可用) /// </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 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", "AliasFileName": "8.webp", "FileId": "914ad20995d34178b34ec264b23f5f52", "SendByte": "iVBORw0KGgrkJggg==" } */ var dataObj = new { FileName = fileName, FormId = formId, IsLast = "true", InterId = id, // SELECT * FROM T_PUR_POORDER WHERE FBILLNO='CGDD001939' BillNo = billNo, 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); } /// <summary> /// 附件分块上传(大于6M的文件可用) /// </summary> [TestMethod] public void BlockUploadTest() { // 星空站点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 fileName = "6.webp"; var filePath = @"C:\Users\Public\Pictures\Sample Pictures\6.webp"; var blockSize = 1024 * 1024; // 分块大小:1M var fileId = string.Empty; using (var fsRead = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { if (fsRead.Length <= 0) { throw new Exception("文件内容不允许为空。"); } while (true) { // 放里面声明,第一次填入完整字节数据,第二次若只填充一部分,此时后面的直接数组并不会被清空。 var content = new byte[blockSize]; int size = fsRead.Read(content, 0, content.Length); if (size == 0) { break; } var isLast = (size != blockSize); var uploadBytes = new byte[size]; Array.Copy(content, uploadBytes, size); var fileBase64String = Convert.ToBase64String(uploadBytes); /* { "FileName": "8.webp", "FormId": "PUR_PurchaseOrder", "IsLast": "true", "InterId": "107361", "BillNo": "CGDD001939", "AliasFileName": "8.webp", "FileId": "914ad20995d34178b34ec264b23f5f52", "SendByte": "iVBORw0KGgrkJggg==" } */ var dataObj = new { FileName = fileName, FormId = formId, IsLast = isLast.ToString(), InterId = id, BillNo = billNo, AliasFileName = fileName, FileId = fileId, 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); fileId = responseDto.Result.FileId; // 第一个分块上传时,FileId是空,从第二个分块开始,FileId不能再为空。 if (isLast) { break; } } } Assert.IsTrue(true); } #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 * FROM T_BAS_ATTACHMENT WHERE FBILLTYPE='PUR_PurchaseOrder' AND FINTERID='107361' ORDER BY FCREATETIME
---------------------------------------------------------------------------------------------------------
【金蝶云星空BOS二次开发案例演示】https://vip.kingdee.com/article/94751030918525696
二开案例.WebApi.文件服务.附件上传
【应用场景】WebApi,附件上传。【案例演示】采购订单,使用WebApi接口,上传单据头附件。【准备工作】新建一张采购订单,如下图。【实现步...
点击下载文档
本文2024-09-23 03:57:56发表“云星空知识”栏目。
本文链接:https://wenku.my7c.com/article/kingdee-k3cloud-162234.html
您需要登录后才可以发表评论, 登录登录 或者 注册
最新文档
热门文章