2

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

            }

在 utility.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;
    }

但返回空值。

有人知道该怎么做吗?

谢谢

4

3 回答 3

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 登录用户名和密码。

于 2014-07-23T13:01:35.717 回答
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}");
于 2017-04-14T16:02:11.247 回答
1

这真的帮助了我。我的 URI 被构建为:

string uri = string.Format("{0}/secure/attachmentzip/{1}.zip?&os_username={2}&os_password={3}", jira, issue.id, user, password);
于 2015-05-21T17:47:23.133 回答