【问题标题】:Fogbugz API - unable to upload file (c# implementation)Fogbugz API - 无法上传文件(c#实现)
【发布时间】:2013-12-03 11:07:22
【问题描述】:

我无法使用fogbugz API 上传附件。我遵循了fogbugz 文档,但也许我错了。发生的错误是: Ex.Message = "给定的键不在字典中。"

StackTrace = " 在 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) 在 c:\Dev\RapidAppsFogbugz\trunk\Business\Services\Fogbugz。 cs:第 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;
            }

【问题讨论】:

    标签: c# fogbugz-api


    【解决方案1】:

    今天早上我遇到了同样的错误,但在查看 FBApi.cs 的源代码时能够修复它。

    简而言之,上传文件的代码依赖于在上传文件时存在的特定密钥,而这在任何地方都没有明确解释。

    这些键是:

    • name(即File1
    • filename(要上传的磁盘上文件的路径——不言自明)
    • contenttype(我用过,"multipart/form-data"FogBugz XML API documentation
    • 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:\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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-14
      • 2017-02-22
      • 2016-04-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多