【分享】第三方系统上传、下载金蝶云文件
金蝶云社区-云社区用户p5517068
云社区用户p5517068
10人赞赏了该文章 6,153次浏览 未经作者许可,禁止转载编辑于2018年06月28日 11:14:35

第三方系统,需要上传文件至金蝶云系统,或者从金蝶云系统下载指定文件,下面以C# .net 为例,由本人提供示例组件,仅供大家参考,不负最终使用责任。组件下载见附件。
一:上传。
1,通过WEB API 登录接口获取上下文中的UserToken。
2,调用金蝶云文件上传接口将文件分段上传至金蝶云系统。
3,上传成功返回文件ID。

主代码简介:
[code] ///


/// 获取UserToken
///

///
private string GetToken()
{
string result = string.Empty;
try
{
APIClient client = new APIClient(ServerUrl,DBID,UserName,Password,cookieContainer);
string lresult = client.Login();
JObject resultObj = JObject.Parse(lresult);
if (resultObj["LoginResultType"].ToString() == "1")
{
result = resultObj["Context"]["UserToken"].ToString();
}
}
catch (Exception ex)
{
return null;
}
return result;
}[/code][code] ///
/// 上传接口
///

/// 上传的文件物理路径
/// 上传之后的文件ID
public string Upload(string filePath)
{
string token = GetToken();
bool bResult = false;
UploadUrl = ServerUrl + UploadUrl;
string fileId = string.Empty;
int MAXSIZE = 1024 * 4;//分批上传文件最大4M
long size = 0;
FileInfo fi = new FileInfo(filePath);
size = fi.Length;
string fileName = fi.Name;
int startIndex = 0;
byte[] newFile = null;
while (size > MAXSIZE) //分块上传
{
newFile = new byte[MAXSIZE];
string tempUrl = UploadUrl;

using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
fs.Seek(startIndex, SeekOrigin.Current);
fs.Read(newFile, 0, newFile.Length);
}
tempUrl = string.Format("{0}?fileName={1}&fileId={2}&token={3}&last={4}", UploadUrl, fileName, fileId, token, size > MAXSIZE ? true : false);
if (!Upload(newFile, tempUrl, ref fileId))
return string.Empty;
size = size - MAXSIZE;
startIndex += MAXSIZE;
newFile = null;
}
if (size > 0)
{
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
newFile = new byte[size];
fs.Seek(startIndex, SeekOrigin.Current);
fs.Read(newFile, 0, newFile.Length);
}
UploadUrl = string.Format("{0}?fileName={1}&fileId={2}&token={3}&last={4}", UploadUrl, fileName, fileId, token, true);
bResult = Upload(newFile, UploadUrl, ref fileId);
}
return bResult ? fileId : string.Empty;
}[/code]第三方调用示例:
[code]

//上传

Kingdee.UploadDownload.IUploadDownload uploadDownload = new Kingdee.UploadDownload.UploadDownload("http://172.17.2.100:81/K3Cloud/", "59f15438d92bee", "yh", "888888", 2052);
//上传成功返回文件ID
string fileID = uploadDownload.Upload(@"D:\test.pdf");

[/code]

二:下载。
1,通过WEB API 登录接口获取上下文中的UserToken。
2,根据文件ID和UserToken调用金蝶云下载文件接口,将文件下载到本地。

主代码简介:
[code]///


/// 下载接口
///

/// 需要下载文件ID
/// 保存的物理地址
public void Download(string fileId, string saveFilePath)
{
string token = GetToken();
DownloadUrl = string.Format("{0}{1}?fileId={2}&token={3}&ticks={4}&nail={5}", ServerUrl, DownloadUrl, fileId, token, DateTime.Now.Ticks, 0);
try
{
using (HttpClient webClient = new HttpClient(cookieContainer))
{
webClient.DownloadFile(DownloadUrl, saveFilePath);
}
}
catch (Exception ex)
{
//do sth like log
}
}[/code]第三方调用示例:
[code]//下载
Kingdee.UploadDownload.IUploadDownload uploadDownload = new Kingdee.UploadDownload.UploadDownload("http://172.17.2.100:81/K3Cloud/", "59f15438d92bee", "yh", "888888", 2052);
//根据文件ID下载文件
uploadDownload.Download("9338781550444796a4ca81707dff0afb", @"D:\test2.pdf");[/code]