如何使用c#在jira中下载附加文件
本文关键字:下载 文件 jira 何使用 | 更新日期: 2023-09-27 18:13:40
我正在尝试使用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);
}
}
和在实用程序中。下载adimagefromurl这是完整的代码:
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;
}
但是返回null。
有人知道怎么做吗?
谢谢
我找到了在jira API中下载附件的解决方案:我使用tecktalk jira RestClient https://github.com/techtalk/JiraRestClient
代码如下:
using (WebClient webClient = new WebClient())
{
webClient.DownloadFile(new Uri(remoteUri + "?&os_username=usernamehere&os_password=passwordhere"), dPath);
}
对于使用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}");
这真的帮了我大忙。我的URI被构建为:
string uri = string.Format("{0}/secure/attachmentzip/{1}.zip?&os_username={2}&os_password={3}", jira, issue.id, user, password);