gpt4 book ai didi

c# - 命令行 c# 代码有效,但 winforms 代码无效

转载 作者:太空狗 更新时间:2023-10-29 23:45:28 29 4
gpt4 key购买 nike

我正在研究 Adobe Echosign演示 C# winforms 应用程序。我的代码是他们命令行代码的直接副本(经过修改),但是,我的代码在传输数据后返回错误。

这是来自 EchoSign 的命令行代码行得通

public static void sendDocument(string apiKey, string fileName, string formFieldLayerTemplateKey, string recipient)
{
FileStream file = getTestPdfFile(fileName);
secure.echosign.com.FileInfo[] fileInfos = new secure.echosign.com.FileInfo[1];
fileInfos[0] = new secure.echosign.com.FileInfo(fileName, null, file);
SenderInfo senderInfo = null;
string[] recipients = new string[1];
recipients[0] = recipient;
DocumentCreationInfo documentInfo = new DocumentCreationInfo(
recipients,
testPrefix + Path.GetFileName(file.Name),
testMessage,
fileInfos,
SignatureType.ESIGN,
SignatureFlow.SENDER_SIGNATURE_NOT_REQUIRED
);
if (formFieldLayerTemplateKey != null)
{
secure.echosign.com.FileInfo[] formFieldLayerTemplates = new secure.echosign.com.FileInfo[1];
formFieldLayerTemplates[0] = new secure.echosign.com.FileInfo(formFieldLayerTemplateKey);
documentInfo.formFieldLayerTemplates = formFieldLayerTemplates;
}
DocumentKey[] documentKeys;
documentKeys = ES.sendDocument(apiKey, senderInfo, documentInfo);
Console.WriteLine("Document key is: " + documentKeys[0].documentKey);
}

这是我的代码块,从他们的系统返回一个错误:

public static void sendDocument(string apiKey, string fileName, string formFieldLayerTemplateKey, string recipient)
{
try
{
SenderInfo senderInfo = new SenderInfo();
senderInfo = null;

FileStream FileToSign = getTestPdfFile(fileName);
byte[] bytes = System.IO.File.ReadAllBytes("C:\\PROJECTS\\TestFile.pdf");


secure.echosign.com.FileInfo[] fileInfos = new secure.echosign.com.FileInfo[1];
fileInfos[0] = new EchoSignTest.secure.echosign.com.FileInfo();
fileInfos[0].fileName = fileName;
fileInfos[0].mimeType = null;
fileInfos[0].file = bytes;

RecipientInfo[] docRecipient = new RecipientInfo[1];
docRecipient[0] = new RecipientInfo();
docRecipient[0].email = recipient;

DocumentCreationInfo documentInfo = new DocumentCreationInfo();
documentInfo.recipients = docRecipient;
documentInfo.name = testPrefix + Path.GetFileName(FileToSign.Name);
documentInfo.message = testMessage;
documentInfo.fileInfos = fileInfos;
documentInfo.signatureType = SignatureType.ESIGN;
documentInfo.signatureFlow = SignatureFlow.SENDER_SIGNATURE_NOT_REQUIRED;

if (formFieldLayerTemplateKey != null)
{
secure.echosign.com.FileInfo[] formFieldLayerTemplates = new secure.echosign.com.FileInfo[1];

formFieldLayerTemplates[0] = new secure.echosign.com.FileInfo();
formFieldLayerTemplates[0].formKey = formFieldLayerTemplateKey;
documentInfo.formFieldLayerTemplates = formFieldLayerTemplates;
}

EchoSignDocumentService19PortTypeClient ES = new EchoSignDocumentService19PortTypeClient();
DocumentKey[] documentKeys = new DocumentKey[1];

documentKeys = ES.sendDocument(apiKey, senderInfo, documentInfo);
Console.WriteLine("Document key is: " + documentKeys[0].documentKey);
}
catch (NullReferenceException ex)
{
string errMessage = ex.Message;
}

catch (Exception ex)
{
string errMessage = ex.Message;
}
}

这两个代码块有什么不同?该错误可能存在于 FileInfo[]DocumentCreationInfo() block 中。我可能没有按照系统要求创建对象。

如有任何建议,我们将不胜感激。

最佳答案

错误似乎是将您读取的文档的字节直接分配给了 fileInfos[0].file 变量。在documentation for FileInfo它声明文件参数必须是使用 base64 编码的原始文件内容,但您分配原始文件内容时未对其进行编码。当像第一个示例(命令行示例)中那样使用文件流调用构造函数时,构造函数似乎会自动处理此问题。您可以尝试在 Winforms 示例中更改这些行:

    FileStream FileToSign = getTestPdfFile(fileName);
byte[] bytes = System.IO.File.ReadAllBytes("C:\\PROJECTS\\TestFile.pdf");


secure.echosign.com.FileInfo[] fileInfos = new secure.echosign.com.FileInfo[1];
fileInfos[0] = new EchoSignTest.secure.echosign.com.FileInfo();
fileInfos[0].fileName = fileName;
fileInfos[0].mimeType = null;
fileInfos[0].file = bytes;

进入此代码并尝试是否可行:

    FileStream FileToSign = getTestPdfFile(fileName);


secure.echosign.com.FileInfo[] fileInfos = new secure.echosign.com.FileInfo[1];
fileInfos[0] = new secure.echosign.com.FileInfo(fileName, null, FileToSign);

您应该使用提供的构造函数而不是直接赋值,以确保正确处理所有变量/参数。

您在关于不带 3 个参数的构造函数的评论中提到的错误可能是 EchoSignTest. 构造函数调用前前缀的结果,因为这似乎是您自己程序的命名空间而不是所提供 API 的正确命名空间。

关于c# - 命令行 c# 代码有效,但 winforms 代码无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25451197/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com