gpt4 book ai didi

azure - 尝试将图像附加到 Azure DevOps wiki 页面时出错

转载 作者:行者123 更新时间:2023-12-03 01:22:44 24 4
gpt4 key购买 nike

我已通过独立的 C# 桌面应用程序成功使用 Azure DevOps API 创建多个 wiki 页面。现在我尝试将图像(当前存储在本地)附加到 wiki(按照 https://learn.microsoft.com/en-us/rest/api/azure/devops/wiki/attachments/create?view=azure-devops-rest-6.0 ),但出现错误

The wiki attachment creation failed with message : The input is not avalid Base-64 string as it contains a non-base 64 character, more thantwo padding characters, or an illegal character among the paddingcharacters.

这是我用来读取图像文件并将其转换为 Base64 字符串的代码 - 这是正确的吗?

string base64String = null; 
string img = File.ReadAllText(att.Value);
byte[] byteCredentials = UTF8Encoding.UTF8.GetBytes(img);
base64String= Convert.ToBase64String(byteCredentials);

然后我为 API 调用创建“内容”

string data = @"{""content"": """ + base64String + @"""}";

并运行 API 调用

string url = "https://dev.azure.com/{organization}/{project}/_apis/wiki/wikis/{wikiIdentifier}/attachments?name=Image.png&api-version=6.0";
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.ContentType = "application/octet-stream";
request.Method = "PUT";
request.Proxy.Credentials = CredentialCache.DefaultCredentials;

request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{1}", "AzurePAT"))));

if (data != null)
{
using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
{
writer.Write(data);
}
}

HttpWebResponse response = request.GetResponse() as HttpWebResponse;

string result = string.Empty;
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
result = reader.ReadToEnd();
}

有人能看出这有什么问题吗?使用编码的 Base64 字符串设置“内容”JSON 的概念似乎没有记录,那么我是否正确完成了它?

任何帮助或建议都很高兴,谢谢

最佳答案

图像文件不包含文本,它是一个二进制文件,调用File.ReadAllText可能会弄乱编码。尝试:

var img = File.ReadAllBytes(att.Value);
var base64String = Convert.ToBase64String(img);

此外,请求正文只是一个字符串。您正在传递 JSON。您的代码应如下所示:

using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
{
writer.Write(base64String);
}

关于azure - 尝试将图像附加到 Azure DevOps wiki 页面时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68586693/

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