0

我正在使用 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);
        }
        }
    }
    }
}
4

2 回答 2

0

第一步是获取一个handel,就像您对POST /attask/api/upload所做的任何其他api调用一样,这是您包含文件的地方,它将被上传到Workfront的临时文件夹(AtTask) , Workfront 将返回handel,然后您执行第二步更新任务,包括handel,文件将发布到它。

有关更多参考,请查看https://developers.attask.com/api-docs/https://developers.attask.com/api-docs/code-samples/

于 2015-03-03T21:08:52.727 回答
0
public static string HttpUploadFile(string url, string filename, byte[] file, string paramName, string contentType, NameValueCollection nvc)
{
        string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
        byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");

        HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
        wr.ContentType = "multipart/form-data; boundary=" + boundary;
        wr.Method = "POST";
        wr.KeepAlive = true;
        wr.Credentials = System.Net.CredentialCache.DefaultCredentials;

        Stream rs = wr.GetRequestStream();

        string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
        foreach (string key in nvc.Keys)
        {
            rs.Write(boundarybytes, 0, boundarybytes.Length);
            string formitem = string.Format(formdataTemplate, key, nvc[key]);
            byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
            rs.Write(formitembytes, 0, formitembytes.Length);
        }
        rs.Write(boundarybytes, 0, boundarybytes.Length);

        string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
        string header = string.Format(headerTemplate, paramName, filename, contentType);
        byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
        rs.Write(headerbytes, 0, headerbytes.Length);

                    rs.Write(file, 0, file.Length);

        byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
        rs.Write(trailer, 0, trailer.Length);
        rs.Close();

        WebResponse wresp = null;
        string ret = null;
        try
        {
            wresp = wr.GetResponse();
            Stream stream2 = wresp.GetResponseStream();
            StreamReader reader2 = new StreamReader(stream2);

            ret = reader2.ReadToEnd();
        }
        catch (Exception ex)
        {
            if (wresp != null)
            {
                wresp.Close();
                wresp = null;
            }
        }
        finally
        {
            wr = null;
        }

        return ret;
}

 

JToken ret = HttpUploadFile("/upload", file_name, file_bytes, "uploadedFile", "", new NameValueCollection());
于 2018-11-19T14:50:05.020 回答