在VBA中使用XML-RPC向Bugzilla添加附件 [英] Add an attachment to Bugzilla using XML-RPC in VBA

查看:141
本文介绍了在VBA中使用XML-RPC向Bugzilla添加附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个Excel宏,该宏允许在Bugzilla实例中创建Bug。
经过反复试验,现在可以正常工作了。
我想增强客户端,以便还可以将屏幕快照添加到新创建的错误中。



我使用的环境有些棘手:
我必须使用MS Excel执行任务。
由于Excel无法理解XML-RPC,因此我下载了一个接口DLL(来自xml-rpc.net的CookComputing.XmlRpcV2.dll),该XML使可从.NET访问的XML-RPC接口。
然后,我创建了一个可以从Excel宏调用的附加DLL(使用COM互操作)。



如前所述,这对于诸如浏览之类的任务都可以正常工作或添加新的错误。
但是,当在错误中添加附件时,必须将图像转换为base64数据类型。尽管这似乎可以正常工作,并且尽管屏幕快照的创建似乎成功,但是图像似乎已损坏并且无法显示。



这是我要添加的内容图像:
Bugzilla add_attachment方法接受一个结构作为输入:
http://www.bugzilla.org/docs/4.0/en/html/api/Bugzilla/WebService/Bug.html#add_attachment
此类型在C#中定义,并且在VBA中也可见。



这是结构体定义:

  [ClassInterface(ClassInterfaceType.AutoDual)] 
公共类TAttachmentInputData
{
公共字符串[] ids;
个公共字符串数据; // base64编码的数据
public string file_name;
公共字符串摘要;
public string content_type;
个公共字符串注释;
public bool is_patch;
public bool is_private;

public void addId(int id)
{
ids = new string [1];
ids [0] = id.ToString();
}

public void addData(string strData)
{
try
{
byte [] encData_byte = new byte [strData.Length ];
encData_byte = System.Text.Encoding.ASCII.GetBytes(strData);
字符串encodingData = Convert.ToBase64String(encData_byte);
data = new Byte [System.Text.Encoding.ASCII.GetBytes(encodedData).Length];
data = System.Text.Encoding.ASCII.GetBytes(encodedData);
}
catch(Exception e)
{
throw new Exception( base64Encode中的错误 + e.Message);
}
}

这是我宏中想要的部分添加附件:

 昏暗的附件结构作为新的TAttachmentInputData 
fname = attachmentFileName

annexsStruct .file_name = GetFilenameFromPath(fname)
annexStruct.is_patch = False
attachmentStruct.is_private = False
'多个其他定义

打开fname作为二进制文件#1
annexsStruct.addData(Input(LOF(1),#1))
关闭#1
annexsStruct.file_name = GetFilenameFromPath(fname)
调用BugzillaClass.add_attachment(attachmentsStruct)


其中的BugzillaClass是从我的DLL暴露给Excel VBA的接口。
方法 add_attachment 是指XML-RPC方法 add_attachment



我认为我的问题是从二进制文件到base64的转换。
这是使用C#DLL中的 addData 方法完成的。
转换正确吗?
知道为什么图像损坏了吗?

解决方案

我认为问题在于您正在读取二进制数据宏,但是addData方法需要一个字符串。尝试将addData中的参数声明为byte []。


I am currently developing an Excel macro which allows creating Bugs in a Bugzilla instance. After some trial and error this now turns out to work fine. I wanted to enhance the client so that it's also possible to add screenshots to the newly created bug.

The environment I'm using is a little bit tricky: I have to use MS Excel for my task. As Excel does not understand XML-RPC, I downloaded an interface DLL (CookComputing.XmlRpcV2.dll from xml-rpc.net) which makes the XML-RPC interface accessible from .NET. Then I created an additional DLL which can be called from Excel macros (using COM interop).

As already mentioned, this is working fine for tasks like browsing or adding new bugs. But when adding an attachment to the bug, the image must be converted into a base64 data type. Although this seems to work fine and although the creation of the screenshot seems to succeed, the image seems to be corrupted and cannot be displayed.

Here's what I do to add the image: The Bugzilla add_attachment method accepts a struct as input: http://www.bugzilla.org/docs/4.0/en/html/api/Bugzilla/WebService/Bug.html#add_attachment. This type was defined in C# and is visible also in VBA.

This is the struct definition:

[ClassInterface(ClassInterfaceType.AutoDual)]
public class TAttachmentInputData
{
    public string[] ids;
    public string data; // base64-encoded data
    public string file_name;
    public string summary;
    public string content_type;
    public string comment;
    public bool is_patch;
    public bool is_private;

    public void addId(int id)
    {
        ids = new string[1];
        ids[0] = id.ToString();
    }

    public void addData(string strData)
    {
        try
        {
            byte[] encData_byte = new byte[strData.Length];
            encData_byte = System.Text.Encoding.ASCII.GetBytes(strData);
            string encodedData = Convert.ToBase64String(encData_byte);
            data = new Byte[System.Text.Encoding.ASCII.GetBytes(encodedData).Length];
            data = System.Text.Encoding.ASCII.GetBytes(encodedData);
        }
        catch (Exception e)
        {
            throw new Exception("Error in base64Encode" + e.Message);
        }
    }

This is the part in my macro where I would like to add the attachment:

    Dim attachmentsStruct As New TAttachmentInputData
    fname = attachmentFileName

    attachmentsStruct.file_name = GetFilenameFromPath(fname)
    attachmentsStruct.is_patch = False
    attachmentsStruct.is_private = False
    'multiple other definitions

    Open fname For Binary As #1
    attachmentsStruct.addData (Input(LOF(1), #1))
    Close #1
    attachmentsStruct.file_name = GetFilenameFromPath(fname)
    Call BugzillaClass.add_attachment(attachmentsStruct)

Where BugzillaClass it the interface exposed from my DLL to Excel VBA. The method add_attachment refers to the XML-RPC method add_attachment.

I assume that my problem is the conversion from the binary file into base64. This is done using the addData method in my C# DLL. Is the conversion done correctly there? Any idea why the images are corrupted?

解决方案

I think the issue is that you are reading in binary data in the macro, but the addData method is expecting a string. Try declaring the parameter in addData as byte[].

这篇关于在VBA中使用XML-RPC向Bugzilla添加附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆