二开案例.WebApi.文件服务.文件上传

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

二开案例.WebApi.文件服务.文件上传

【应用场景】

WebApi,仅上传文件,文件不绑定单据。



【案例演示】

使用WebApi接口,上传文件



【实现步骤】

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

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

接口名称:UploadFile

请求Url:

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

请求数据包:

{
    "FileName": "8.webp",
    "IsLast": "true",
    "FileId": "914ad20995d34178b34ec264b23f5f52",
    "SendByte": "iVBORw0KGgrkJggg=="
}


响应数据包:

{
    "Result": {
        "ResponseStatus": {
        "IsSuccess": true,
        "Errors": [],
        "SuccessEntitys": [],
        "SuccessMessages": [],
        "MsgCode": 0
    },
    "FileId": "b1058e9c45864c17a49dd7ee6187d1de",
    "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 FileServiceFileUploadTest
    {
        /// <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 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",
"IsLast": "true",
"FileId": "914ad20995d34178b34ec264b23f5f52",
"SendByte": "iVBORw0KGgrkJggg=="
}
             */
            var dataObj = new
            {
                FileName = fileName,
                IsLast = "true",
                SendByte = fileBase64String
            };
            var data = JsonConvert.SerializeObject(dataObj);
            //Console.WriteLine(data);
            var result = client.UploadFile(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:

-- 查询文件信息
SELECT * FROM dbo.T_BAS_FILESERVERFILEINFO WHERE FFILENAME='8.webp' ORDER BY FCREATEDATE

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













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


二开案例.WebApi.文件服务.文件上传

【应用场景】WebApi,仅上传文件,文件不绑定单据。【案例演示】使用WebApi接口,上传文件。【实现步骤】<1>学习附件上传接口。学习资料:...
点击下载文档
确认删除?
回到顶部
客服QQ
  • 客服QQ点击这里给我发消息