Anonymní profil JIzzy – Programujte.com
 x   TIP: Přetáhni ikonu na hlavní panel pro připnutí webu

Anonymní profil JIzzy – Programujte.comAnonymní profil JIzzy – Programujte.com

 

Příspěvky odeslané z IP adresy 85.71.40.–

JIzzy
.NET › HttpWebRequest multipart/for…
12. 8. 2012   #161420

   

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); });

 

 

Hostujeme u Českého hostingu       ISSN 1801-1586       ⇡ Nahoru Webtea.cz logo © 20032024 Programujte.com
Zasadilo a pěstuje Webtea.cz, šéfredaktor Lukáš Churý