我正在使用 C# API 示例的修改版本将任务添加到现有的 Workfront (AtTask) 任务。我也想上传和下载文件附件。
从文档中可以看出,上传是一个两步过程,步骤 1 上传文件,步骤 2 将上传的文件附加到任务中。我对如何执行第二步有所了解 - 发布带有文件名、句柄(来自上传)、对象类型(TASK)、对象 ID 和 currentVersion 的 JSON 令牌。我不明白的是第 1 步,即文件的实际上传。
我正在创建一个需要附加到任务的 PDF 文件。任务完成后,将添加一个我需要下载的新文档。
有没有人有任何 C# 代码来执行上传或下载?
到目前为止,这是我的代码:
public JToken DoUpload(string path, string opportunityID, string description, params string[] parameters)
{
List<string> list = parameters.ToList();
if (!path.StartsWith("/"))
{
path = "/" + path;
}
string fullUrl = url + path + ToQueryString(parameters);
string boundary = "------" + DateTime.Now.Ticks.ToString("x");
WebRequest request = HttpWebRequest.CreateDefault(new Uri(fullUrl));
request.ContentType = "multipart/form-data; boundary=" + boundary;
request.Method = "POST";
using (var requestStream = request.GetRequestStream())
{
using (var writer = new StreamWriter(requestStream))
{
writer.WriteLine(string.Format("Content-Disposition: form-data; name=\"{0}\" filename=\"{1}\"", "uploadedFile", "RFQ" + opportunityID + ".html"));
writer.WriteLine("Content-Type: text/html; charset=UTF-8");
writer.WriteLine();
writer.WriteLine(description);
using (WebResponse response = request.GetResponse())
{
using (Stream responseStream = response.GetResponseStream())
{
return ReadResponse(responseStream);
}
}
}
}
}