string boundary = "--" + DateTime.Now.Ticks.ToString();
string newline = Environment.NewLine;
foreach (JIRAAttachments attachm in attachments.attachments)
{
byte[] buffer = null;
using (BinaryReader reader = new BinaryReader(File.OpenRead(@"..\Attachments\" + attachm.filename)))
{
buffer = reader.ReadBytes((int)reader.BaseStream.Length);
}
request = HttpWebRequest.Create(logInformation.GetUri() + "/rest/api/2/issue/" + key + "/attachments") as HttpWebRequest;
request.Headers.Add("Authorization: Basic " + logInformation.GetEncodeAuthentication());
request.Method = "POST";
request.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);
request.Headers.Add("X-Atlassian-Token: nocheck");
request.KeepAlive = true;
request.ContentLength = buffer.Length;
request.SendChunked = true;
request.Proxy = wp;
using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
{
// Part header.
writer.WriteLine(boundary);
writer.WriteLine("Content-Disposition: form-data; name=\"" + attachm.author.name + newline);
writer.WriteLine(attachm.author.name);
writer.WriteLine(boundary);
writer.WriteLine("Content-Disposition: form-data; name=\"soubor\"; filename=\"" + Path.GetFullPath(@"..\Attachments\" + attachm.filename) + "\"");
writer.WriteLine("Content-Type:" + GetContentType(attachm.filename) + newline);
writer.Flush();
// Part content.
writer.BaseStream.Write(buffer, 0, buffer.Length);
// Part footer.
writer.WriteLine(boundary + "--");
writer.Flush();
}
}
// Delete all attachments in directory
Array.ForEach(Directory.GetFiles(@"..\Attachments\"), delegate(string path) { File.Delete(path); });