简体   繁体   中英

HP ALM C# REST set attachment for requirement

I've seen many different 'answers' to this, none of them complete. This is my current code for my upload function. It should be complete enough for discussion. I'm getting a (500) error when attempting to upload a .png file. The .png file gets consumed by the formbytes stream, but when it gets to the actual request on the last line, it throws the error. HELP!

public void Upload(string uri, string filePath)
    {
        FileInfo fi = new FileInfo(filePath);
        string fileType = fi.Extension.Replace(".", "");
        string slug = fi.Name.Replace(fi.Extension, "");
        string formdataTemplate = "Content-Disposition: form-data; name=\"file\" filename=\"{0}\";\r\nContent-Type: application/octet-stream\r\n\r\n";
        string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
        byte[] boundarybytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
        request.Method = "POST";
        request.ContentType = "multipart/form-data; boundary=" + boundary;
        request.Accept = "application/json";

        if (cookies != null)
        {
        // cookies is a cookie container populated by another process, works for creating ALM requirements
            request.CookieContainer = cookies;
        }

        request.Headers.Add("slug", slug);

        string formitem = "";
        byte[] formbytes = null;
        using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
        {
            using (Stream requestStream = request.GetRequestStream())
            {
                requestStream.Write(boundarybytes, 0, boundarybytes.Length);
                formitem = string.Format(formdataTemplate, Path.GetFileName(filePath));
                formbytes = Encoding.UTF8.GetBytes(formitem);
                requestStream.Write(formbytes, 0, formbytes.Length);
                byte[] buffer = new byte[1024 * 4];
                int bytesLeft = 0;

                while ((bytesLeft = fileStream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    requestStream.Write(buffer, 0, bytesLeft);
                }

            }
        }
            //request.Headers.Add(formbytes);

        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { }

    }        

The answer is that I had to check-out the requirement to add the attachment. It's a simple post, with no body needed, to :

/rest/domains/[domain]/projects/[project]/requirements/[entityId]/versions/check-out

and you can check-in, also no body needed:

/rest/domains/[domain]/projects/[project]/requirements/[entityId]/versions/check-out

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM