Fogbugz API - unable to upload file (c# implementation)

413 views Asked by At

I can not upload attachments using the fogbugz API. I followed the fogbugz documentation but maybe I am wrong something. The error that occurs is: Ex.Message = "The given key was not present in the dictionary."

StackTrace = " at System.Collections.Generic.Dictionary2.get_Item(TKey key) at UBR.Products.TimeTrakker.Client.Lib.FogBugz.FBApi.CallRESTAPIFiles(String sURL, Dictionary2 rgArgs, Dictionary2[] rgFiles) at UBR.Products.TimeTrakker.Client.Lib.FogBugz.FBApi.Cmd(String cmd, Dictionary2 args, Dictionary2[] files) at Business.Services.Fogbugz.CreateBug(String title, String areaId, String summary, List1 fileData) in c:\Dev\RapidAppsFogbugz\trunk\Business\Services\Fogbugz.cs:line 114"

            public string CreateBug(string title, string areaId, string summary)
            {
                try
                {
                    string bugIdResult;
                    //Build args from Bug object
                    var args = FillArgsDictionaryForCreatingBug(title, GetDefaultProjectID(), areaId, summary);
                    var fileArgs = GetAttachmentArgs(GetAttachmentList());

                    //args.Add("nFileCount", "1");

                    if (fileArgs != null) //WITH ATTACHMENTS
                        bugIdResult = GetBugId(_fogbugz.Cmd("new", args, fileArgs));
                    else //NO ATTACHMENTS
                        bugIdResult = GetBugId(_fogbugz.Cmd("new", args));

                    return bugIdResult;
                }
                catch (Exception ex)
                {
                    throw new Exception(typeof(Fogbugz).ToString() + " : Method = CreateBug", ex);
                }
            }

            private List<byte[]> GetAttachmentList()
            {
                List<byte[]> fileData = null;

                if (Request.Files.Count > 0 && Request.Files[0].ContentLength > 0)
                {
                    fileData = new List<byte[]>();

                    for (int i = 0; i < Request.Files.Count; i++)
                    {
                        HttpPostedFileBase file = Request.Files[i];
                        if (file.ContentLength > 0)
                        {
                            using (var binaryReader = new BinaryReader(file.InputStream))
                            {
                                fileData.Add(binaryReader.ReadBytes(file.ContentLength));
                            }
                        }
                    }
                }

             private Dictionary<string, string> FillArgsDictionaryForCreatingBug(string title, string projectId, string areaId, string summary)
                {
                    var args = new Dictionary<string, string>
                    {
                        {"sTitle", title},
                        {"ixProject", projectId},
                        {"ixArea", areaId},
                        {"sEvent", summary}
                    };
                    return args;
                }

            private Dictionary<string,byte[]>[] GetAttachmentArgs(List<byte[]> fileData)
            {
                Dictionary<string, byte[]>[] result = null;
                if (fileData != null)
                {
                    var fileArgs = new List<Dictionary<string, byte[]>>();
                    for (int i = 0; i < fileData.Count; i++)
                    {
                        var dictionary = new Dictionary<string, byte[]> { { "File" + (i+1).ToString(), fileData[i] } };
                        fileArgs.Add(dictionary);
                    }
                    result = fileArgs.ToArray();
                }

                return result;
            }
1

There are 1 answers

0
Anthony Forloney On

I was struggling with the same error this morning but was able to fix it when looking at the source code for the FBApi.cs.

In short, the code to upload the file is relying on specific key's to be present when uploading a file which was not clearly explained anywhere.

These key's are:

  • name (ie, File1)
  • filename (path to file on disk to upload -- self explanatory)
  • contenttype (I had used, "multipart/form-data" per the FogBugz XML API documentation)
  • data (ie, the actual byte[] of the file contents).

Once these were supplied, the exception went away and my test file was successfully uploaded.

For illustration purposes, I have included an example of my working code below,

Dictionary<String, String> caseOptions = new Dictionary<String, String>();
Dictionary<String, Byte[]> PDF = new Dictionary<String,Byte[]>();
Dictionary<String, Byte[]>[] files = new Dictionary<String, Byte[]>[1];
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
// below will log into our FogBugz URL to use the FBApi
FogBugz.Init();

string fileName = @"C:\path\to\test.pdf";

// setup any necessary values
caseOptions.Add("sEvent", "Attaching PDF file");
caseOptions.Add("ixBug", "9999");

// setup required keys
PDF.Add("name", encoding.GetBytes("File1"));
PDF.Add("filename", encoding.GetBytes(fileName));
PDF.Add("contenttype", encoding.GetBytes("multipart/form-data"));
PDF.Add("data", File.ReadAllBytes(fileName));

files[0] = PDF;

FogBugz.fb.XCmd("edit", caseOptions, files);