如何使用C#在jira中下载附件文件

时间:2014-07-18 09:21:17

标签: c# jira-rest-api

我尝试使用jira techtalk从jira下载附件文件以获取问题和附件。

foreach (var img in issue.fields.attachment)
            {
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                dynamic item = serializer.Deserialize<object>(img.ToString());

                System.Drawing.Image attachImg = Utilities.DownloadImageFromUrl(item["content"].ToString());
                if (attachImg != null) {
                    var sPath = Path.Combine(Server.MapPath("~/Content/Uploads/"), item["filename"].ToString());
                    attachImg.Save(sPath);
                }

            }

并在utilities.downloadimagefromurl中这是完整的代码:

public static System.Drawing.Image DownloadImageFromUrl(string imageUrl)
    {
        System.Drawing.Image image = null;

        try
        {
            System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(imageUrl);
            webRequest.AllowWriteStreamBuffering = true;
            webRequest.Timeout = 30000;

            System.Net.WebResponse webResponse = webRequest.GetResponse();

            System.IO.Stream stream = webResponse.GetResponseStream();

            image = System.Drawing.Image.FromStream(stream);

            webResponse.Close();
        }
        catch (Exception ex)
        {
            return null;
        }

        return image;
    }

但是返回空值。

有人知道怎么做吗?

由于

3 个答案:

答案 0 :(得分:2)

我找到了在jira API中下载附件的解决方案:我使用tecktalk Jira Rest Client https://github.com/techtalk/JiraRestClient

以下是代码:

using (WebClient webClient = new WebClient())
{

    webClient.DownloadFile(new Uri(remoteUri + "?&os_username=usernamehere&os_password=passwordhere"), dPath);

}

使用您的jira登录用户名和密码。

答案 1 :(得分:2)

对于使用RestSharp的任何人:

using RestSharp.Extensions;

Uri uri = new Uri($"http://{jira}/secure/attachment/{attachmentID.ToString()}/" + 
    $"?&os_username={userName}&os_password={password}");

IRestRequest request = new RestRequest(uri, Method.GET);

restClient.DownloadData(request).SaveAs($"{saveLocation}");

答案 2 :(得分:1)

这对我很有帮助。我的URI构建为:

string uri = string.Format("{0}/secure/attachmentzip/{1}.zip?&os_username={2}&os_password={3}", jira, issue.id, user, password);