我无法使用fogbugz API 上传附件。我遵循了fogbugz 文档,但也许我错了。发生的错误是:Ex.Message = "字典中不存在给定的键。"
StackTrace = "在 System.Collections.Generic.Dictionary 2.get_Item(TKey key)
at UBR.Products.TimeTrakker.Client.Lib.FogBugz.FBApi.CallRESTAPIFiles(String sURL, Dictionary
2 rgArgs,字典2[] rgFiles)
at UBR.Products.TimeTrakker.Client.Lib.FogBugz.FBApi.Cmd(String cmd, Dictionary
2 args,字典2[] files)
at Business.Services.Fogbugz.CreateBug(String title, String areaId, String summary, List
1 文件数据) 在 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;
}