Web Service 上传附件Json格式【分享】
金蝶云社区-云社区用户4u731234
云社区用户4u731234
4人赞赏了该文章 8,305次浏览 未经作者许可,禁止转载编辑于2015年07月01日 01:03:34

先上图,有图有真相。
如下图:共三条数据,第一条是通过界面中增加的数据,第2,3条是通过系统Web Service调用添加的附件数据。

C#调用代码
[code]//Web Service不引用SDK方式 新增【附件明细】 (Cloud5.0)
//新定义业务对象,1、继承BOS_Attachment,2、对应的保存表名改成T_BAS_ATTACHMENT
//新建立.net Winform程序Copy代码,Newtonsoft.Json.dll 按代码中描述进行相关修改即可运行成功
//注:此Json编码方式客户端调用,无需引用SI站点发布的Web Service代理类。类似引用地址: http://localhost:26794/K3CloudServiceInterface/metadata 可以正常访问到发布的接口即可

//通过如下代码,可新增PUR_PurchaseOrder 内码为“100094”编号为“CGDD000047”采购订单的单据头附件。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing;
using Newtonsoft.Json.Linq;

namespace WindowsFormsApplication.WebServiceTest
{
public partial class Form1 : Form
{
private static string urlCommon
{
get
{
//发布的ServiceInterface站点标识(根据个人的发布站点情况进行赋值) 机器名+端口号+名称
string strCommon = "localhost:26794/K3CloudServiceInterface";
return strCommon;
}
}

public Form1()
{
InitializeComponent();
this.Init();
}

private void Init()
{
this.Text = "K/3 Cloud Attachment Test Ver1.0";

Button btn = new Button();
btn.Text = "Attachment Test";
btn.Height = 80;
btn.Width = 100;
btn.Location = new Point(50, 50);
this.Controls.Add(btn);

btn.Click += (o, e) =>
{
this.Invoke();
};
}

public void Invoke()
{
if (Login())
{
MessageBox.Show("Login Success");

string filePath = @"test.txt";

string sFAttachmentName;
string sFAttachmentSize;
string sFExtName;
string sFAttachment;

using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
BinaryReader br = new BinaryReader(fs);
byte[] bFAttachment = br.ReadBytes((int)fs.Length);
sFAttachmentName = fs.Name.Substring(fs.Name.LastIndexOf('\\') + 1);
sFAttachmentSize = Convert.ToString(Math.Round((Decimal)fs.Length / 1024, 2));
sFExtName = fs.Name.Substring(fs.Name.LastIndexOf('.'));
sFAttachment = Convert.ToBase64String(bFAttachment);
}

//业务对象Id 此业务对象的Id,是通过1、继承BOS_Attachment并且2、对应的保存表名改成T_BAS_ATTACHMENT。(这里之所以要这么做,是因为Web Service接口定义,限制了对BOS领域下的业务对象的调用)
string sFormId = "BOS_Attachment_Test";
string sJson = string.Empty;

JObject jObj = new JObject();
jObj.Add("FInterID", "100094");//业务数据内码
//jObj.Add("FEntryKey", "");//业务对象单据体标识
jObj.Add("FEntryInterID", "-1");//-1 代码单据头附件,单据体附件需指定具体业务数据单据体内码,
jObj.Add("FBillNo", "CGDD000047");//业务数据编码
jObj.Add("FBillType", "PUR_PurchaseOrder");//附件关联的业务对象Id
jObj.Add("FAttachmentName", sFAttachmentName);
jObj.Add("FAttachmentSize", sFAttachmentSize);
jObj.Add("FExtName", sFExtName);
jObj.Add("FAttachmentDes", "Web Service Invoke");
jObj.Add("FAttachment", sFAttachment);

JObject jModel = new JObject();
jModel.Add("Creator", "hola");
jModel.Add("NeedUpDateFields", new JArray());
jModel.Add("Model", jObj);

sJson = jModel.ToString();

var ret = Save(sFormId, sJson);

MessageBox.Show(ret);
}
}

///


/// 登陆
///

public static bool Login()
{
HttpClient httpClient = new HttpClient();
httpClient.Url = string.Concat("http://", urlCommon, "/json/syncreply/Auth");
string sLoginData = "{\"provider\":\"credentials\",\"UserName\":\"Administrator\",\"Password\":\"888888\",\"PasswordIsEncrypted\":false,\"RememberMe\":false}";
httpClient.Content = sLoginData;

var result = httpClient.SysncRequest();

var bResult = JObject.Parse(result)["Result"]["ResponseStatus"]["IsSuccess"].Value();
return bResult;
}

///


/// 保存
///

public static string Save(string formId, string content)
{
HttpClient httpClient = new HttpClient();
httpClient.Url = string.Concat("http://", urlCommon, "/json/syncreply/", formId, "_Save");
httpClient.Content = content;

var result = httpClient.SysncRequest();
return result;
}

}
}
[/code]

HttpClient类
[code]using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace WindowsFormsApplication.WebServiceTest
{
///


/// HTTP访问
///

public class HttpClient
{
///
/// URL
///

public string Url { get; set; }
///
/// Content
///

public string Content { get; set; }
///
/// Cookie
///

static CookieContainer Cookie = new CookieContainer();

///


/// HTTP访问
///

public string SysncRequest()
{
HttpWebRequest httpWebRequest = WebRequest.Create(Url) as HttpWebRequest;
httpWebRequest.Method = "POST";
httpWebRequest.CookieContainer = Cookie;
httpWebRequest.ContentType = "application/json";

using (Stream requestStream = httpWebRequest.GetRequestStream())
{
byte[] postData = UnicodeEncoding.UTF8.GetBytes(Content);

requestStream.Write(postData, 0, postData.Length);
requestStream.Flush();
}
using (var repStream = httpWebRequest.GetResponse().GetResponseStream())
{
using (var reader = new StreamReader(repStream))
{
string responseText = reader.ReadToEnd();
JObject jo = JsonConvert.DeserializeObject(responseText) as JObject;
return jo.ToString();
}
}
}

}

}
[/code]

wsfj.png(25.33KB)