之前钉钉是不支持直接接口进行传附件的,只能通过前端获取参数然后点击传输;新接口通过代码封装获取数据流,上传附件然后提交附件,把返回的数据包解析适合于三方的发起格式数据,上传即可,剩下就是通过接口传审批带附件内容的数据。
主要使用的接口:
- 获取钉盘空间信息接口
POST /v1.0/workflow/processInstances/spaces/infos/query HTTP/1.1
Host:api.dingtalk.com
x-acs-dingtalk-access-token:String
Content-Type:application/json{
"userId" : "String",
"agentId" : Long
} - 获取文件上传信息接口
POST /v1.0/storage/spaces/{spaceId}/files/uploadInfos/query?unionId=String HTTP/1.1
Host:api.dingtalk.com
x-acs-dingtalk-access-token:String
Content-Type:application/json{
"protocol" : "String",
"multipart" : Boolean,
"option" : {
"storageDriver" : "String",
"preCheckParam" : {
"md5" : "String",
"size" : Long,
"parentId" : "String",
"name" : "String"
},
"preferRegion" : "String",
"preferIntranet" : Boolean
}
} - 使用OSS的header加签方式上传文件
public static string HttpRequest(string url, string filePath, Dictionary<string, string> headers) {
FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);BinaryReader reader = new BinaryReader(fileStream);reader.BaseStream.Seek(0, SeekOrigin.Begin);byte[] datas = reader.ReadBytes((int)reader.BaseStream.Length);HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);request.Method = "PUT";request.Timeout = 150000;foreach (var header in headers) {request.Headers.Add($"{header.Key}", $"{header.Value}");}Stream requestStream = null;string responseStr = null;try {if (datas != null) {request.ContentLength = datas.Length;requestStream = request.GetRequestStream();requestStream.Write(datas, 0, datas.Length);requestStream.Close();} else {request.ContentLength = 0;}HttpWebResponse response = request.GetResponse() as HttpWebResponse;responseStr = response.Headers.GetValues("x-oss-request-id")[0];} catch (Exception ex) {Console.WriteLine("error");} finally {request = null;requestStream = null;}return responseStr;} - 提交文件
-
POST /v1.0/storage/spaces/{spaceId}/files/commit?unionId=String HTTP/1.1
Host:api.dingtalk.com
x-acs-dingtalk-access-token:String
Content-Type:application/json{
"uploadKey" : "String",
"name" : "String",
"parentId" : "String",
"option" : {
"size" : Long,
"conflictStrategy" : "String",
"appProperties" : [ {
"name" : "String",
"value" : "String",
"visibility" : "String"
} ]
}
}