二开案例.WebApi.文件服务.附件下载
【应用场景】
WebApi,附件下载。
【案例演示】
使用WebApi接口,下载附件。
【实现步骤】
<1>学习附件上传接口。
学习资料:【WebAPI附件上传下载接口】https://vip.kingdee.com/article/296577252589190400
接口名称:AttachmentDownLoad
请求Url:
请求数据包:
{ "FileId": "914ad20995d34178b34ec264b23f5f52", "StartIndex": 0 }
响应数据包:
{ "Result": { "ResponseStatus": { "IsSuccess": true, "Errors": [], "SuccessEntitys": [], "SuccessMessages": [], "MsgCode": 0 }, "StartIndex": 4194304, "IsLast": true, "FileSize": 3570547, "FileName": "8.webp", "FilePart": "iVBORwggg==", "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 FileServiceDownloadTest { /// <summary> /// 附件下载 /// </summary> [TestMethod] public void DownloadTest() { // 星空站点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 fileId = "914ad20995d34178b34ec264b23f5f52"; // SELECT * FROM T_BAS_FILESERVERFILEINFO WHERE FFILENAME='8.webp' var fileName = "8.webp"; var filePath = @"C:\Users\Public\Pictures\Sample Pictures\test\8.webp"; /* { "FileId": "914ad20995d34178b34ec264b23f5f52", "StartIndex": 0 } */ var dataObj = new { FileId = fileId, StartIndex = 0 }; var data = JsonConvert.SerializeObject(dataObj); //Console.WriteLine(data); var result = client.AttachmentDownLoad(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.FilePart)); if (responseDto.Result.IsLast) { // 文件较小,一次性下载完了,保存文件 var fileBytes = Convert.FromBase64String(responseDto.Result.FilePart); File.WriteAllBytes(filePath, fileBytes); Assert.IsTrue(File.Exists(filePath)); } else { // 文件太大,被分块下载,获取所有分块数据后,再保存文件 var list = new System.Collections.Generic.List<byte>(); var fileBytes = Convert.FromBase64String(responseDto.Result.FilePart); list.AddRange(fileBytes); while (!responseDto.Result.IsLast) { // 循环下载,直到文件全部下载完 var dataObjNext = new { FileId = fileId, StartIndex = responseDto.Result.StartIndex }; data = JsonConvert.SerializeObject(dataObjNext); result = client.AttachmentDownLoad(data); Console.WriteLine(result); 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.FilePart)); fileBytes = Convert.FromBase64String(responseDto.Result.FilePart); list.AddRange(fileBytes); } // 下载完了,保存文件 File.WriteAllBytes(filePath, list.ToArray()); Assert.IsTrue(File.Exists(filePath)); } } #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 long StartIndex { get; set; } public bool IsLast { get; set; } public int FileSize { get; set; } public string FileName { get; set; } public string FilePart { get; set; } public string Message { 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 } } }
【功能验证】
执行单元测试,即可下载附件至本地。
---------------------------------------------------------------------------------------------------------
【金蝶云星空BOS二次开发案例演示】https://vip.kingdee.com/article/94751030918525696
二开案例.WebApi.文件服务.附件下载
【应用场景】WebApi,附件下载。【案例演示】使用WebApi接口,下载附件。【实现步骤】<1>学习附件上传接口。学习资料:【WebAPI附件上传下...
点击下载文档
上一篇:危险方法分析下一篇:二开案例.WebApi.文件服务.文件上传
本文2024-09-23 03:59:51发表“云星空知识”栏目。
本文链接:https://wenku.my7c.com/article/kingdee-k3cloud-162436.html
您需要登录后才可以发表评论, 登录登录 或者 注册
最新文档
热门文章