图片上传:
byte[] filedata = File.ReadAllBytes(filepath);//根据路径读取文件数据
调用接口:
string filename = item.Keys.FirstOrDefault();
string filepath = item.Values.FirstOrDefault();
byte[] filedata = File.ReadAllBytes(filepath);
//上传附件请求
string res = HttpPost(FileUrl, filename, "13338", filedata, WorkFlowId, AppKey);//filedata为字节数组类型
/// <summary>
/// HttpPost请求函数
/// </summary>
/// <param name="url"></param>
/// <param name="body"></param>
/// <returns></returns>
public static string HttpPost(string url, string title, string loginId, byte[] file, string workflowId, string appkey)
{
try
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url + "?title=" + title + "&loginId=" + loginId + "&workflowId=" + workflowId + "&appkey=" + appkey);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "Post";
using (var streamWriter = httpWebRequest.GetRequestStream())
{
streamWriter.Write(file, 0, file.Length);//文件流写入body中
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
return result;
}
}
catch (Exception ex)
{
return ex.Message.ToString();
}
}