我无法使用fogbugz API上传附件。我遵循了fogbugz文档,但也许我错了什么。出现的错误是:示例消息="字典中不存在给定的密钥。"
StackTrace="位于c:\Dev\RapidAppsFogbugz\trunk\Business\Services\Fogbugz中的System.Collections.Generic.Dictionary 2.get_Item(TKey key)
at UBR.Products.TimeTrakker.Client.Lib.FogBugz.FBApi.CallRESTAPIFiles(String sURL, Dictionary
2 rgArgs,Dictionary 2[] rgFiles)
at UBR.Products.TimeTrakker.Client.Lib.FogBugz.FBApi.Cmd(String cmd, Dictionary
2 args,Diction 2[] files)
at Business.Services.Fogbugz.CreateBug(String title, String areaId, String summary, List
1 fileData)。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;
}
今天早上我遇到了同样的错误,但在查看FBApi.cs.的源代码时,我修复了它
简而言之,上传文件的代码依赖于上传文件时出现的特定密钥,而这些密钥在任何地方都没有明确解释。
这些密钥是:
name
(即File1
)filename
(要上传的磁盘上文件的路径--不言自明)contenttype
(我使用过,根据FogBugzXMLneneneba API文档"multipart/form-data"
)data
(即文件内容的实际byte[]
)
一旦提供了这些,异常就消失了,我的测试文件也成功上传了。
为了便于说明,我在下面包含了一个我的工作代码示例,
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:pathtotest.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);