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, Dictionary
2 rgArgs, Dictionary2[] rgFiles)
at UBR.Products.TimeTrakker.Client.Lib.FogBugz.FBApi.Cmd(String cmd, Dictionary
2 args, Dictionary2[] files)
at Business.Services.Fogbugz.CreateBug(String title, String areaId, String summary, List
1 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;
}
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 actualbyte[]
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,