C# 通过附加二进制图像内容修补现有OneNote页面

C# 通过附加二进制图像内容修补现有OneNote页面,c#,microsoft-graph-api,onenote,C#,Microsoft Graph Api,Onenote,使用Microsoft Graph v1.0 api和C#,我可以更新(修补)现有OneNote页面。但是,在将图像数据写入“多部分/表单数据”区域后,生成的图像高度和宽度是正确的,但是,除了空图像占位符外,图像不会呈现在页面上 所以问题是,OneNote希望修补程序命令的正确图像格式是什么?文档声明它必须是“二进制图像数据”。File.ReadAllBytes是否足够 以下是迄今为止尝试的格式: string file = @"c:\images\file1.jpg"; var rawDat

使用Microsoft Graph v1.0 api和C#,我可以更新(修补)现有OneNote页面。但是,在将图像数据写入“多部分/表单数据”区域后,生成的图像高度和宽度是正确的,但是,除了空图像占位符外,图像不会呈现在页面上

所以问题是,OneNote希望修补程序命令的正确图像格式是什么?文档声明它必须是“二进制图像数据”。File.ReadAllBytes是否足够

以下是迄今为止尝试的格式:

string file = @"c:\images\file1.jpg";

var rawData = System.IO.File.ReadAllText(file); //attempt1
byte[] bytes = System.IO.File.ReadAllBytes(file);  //attempt2
var byteArray = BitConverter.ToString(bytes);   //attempt3
var byteString = Encoding.ASCII.GetString(bytes);  //attempt4
string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);  //attempt5
string imageDataURL = string.Format("data:image/jpeg;base64,{0}", base64String);  //attempt6

/*构建消息内容*/
StringBuilder sb=新的StringBuilder();
sb.Append(“--MyPartBoundary198374”+”\r\n”);
sb.Append(@“内容处置:表单数据;name=”“Commands”“+”\r\n”);
sb.Append(“内容类型:application/json”+”\r\n“+”\r\n”);
附加(
@"[{
'target':'body',
“操作”:“附加”,
'位置':'之前',
“内容”:”
}]" 
+“\r\n”+“\r\n”);
sb.Append(“--MyPartBoundary198374”+”\r\n”);
sb.追加(@“内容处置:表单数据;名称=”“image1”“+”\r\n”);
sb.Append(“内容类型:image/jpeg”+”\r\n\r\n”);
sb.Append([参见上面的格式]+“\r\n”);
sb.追加(“--MyPartBoundary198374-”);
字符串内容=sb.ToString();
string contentType=“多部分/表单数据;边界=MyPartBoundary198374”;
var result=wait-OneNoteService.UpdatePageAsync(客户端、页面、内容类型、内容);

内部静态异步任务UpdatePageAsync(GraphServiceClient客户端、OnenotePage页面、字符串内容类型、字符串内容)
{
HttpResponseMessage响应=null;
尝试
{
string requestUri=client.Users[ME].Onenote.Pages[page.Id].Content.Request().RequestUrl;
List patchCommands=new List();
HttpRequestMessage请求=新建HttpRequestMessage()
{
方法=新的HttpMethod(“补丁”),
RequestUri=新Uri(RequestUri),
Content=newstringcontent(Content,Encoding.UTF8,“application/json”),
};
request.Content.Headers.Remove(“内容类型”);
request.Content.Headers.TryAddWithoutValidation(“内容类型”,contentType);
//将用户的访问令牌从GraphServiceClient添加到请求中。
等待client.AuthenticationProvider.AuthenticateRequestAsync(请求);
response=wait client.HttpProvider.sendsync(请求);
如果(!response.issucessStatusCode)
{
抛出新的Microsoft.Graph.ServiceException(
新错误
{
Code=response.StatusCode.ToString(),
Message=wait response.Content.ReadAsStringAsync()
});
}
}
捕获(例外情况除外)
{
//TODO:错误处理
Console.WriteLine(例如ToString());
}
返回响应;
}
此帖子中列出的建议没有解决此问题:

一天多来一直在努力解决这个问题。是否有人可以提供PATCH命令所需的正确图像数据格式

谢谢,
罗兰

将前面的评论移到一个答案上,以防其他人碰到这个问题

不要发送多个部分,而是组合尝试5和6,并将结果直接包含在
src
属性中:

string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);  //attempt5
string imageDataURL = string.Format("data:image/jpeg;base64,{0}", base64String);  //attempt6

/* Construct message content */
StringBuilder sb = new StringBuilder();
sb.Append("--MyPartBoundary198374" + "\r\n");
sb.Append(@"Content-Disposition: form-data; name=""Commands"""  + "\r\n");
sb.Append("Content-Type: application/json" + "\r\n" + "\r\n");

sb.Append(
@"[{
    'target':'body',
    'action':'append',
    'position':'before',
    'content':'<img src=" + imageDataURL  + " width=""400"" height=""500""/>'
}]" 

+ "\r\n" + "\r\n");
string base64String=Convert.ToBase64String(字节,0,字节.长度)//尝试5
字符串imageDataURL=string.Format(“数据:image/jpeg;base64,{0}”,base64String)//尝试6
/*构建消息内容*/
StringBuilder sb=新的StringBuilder();
sb.Append(“--MyPartBoundary198374”+”\r\n”);
sb.Append(@“内容处置:表单数据;name=”“Commands”“+”\r\n”);
sb.Append(“内容类型:application/json”+”\r\n“+”\r\n”);
附加(
@"[{
'target':'body',
“操作”:“附加”,
'位置':'之前',
“内容”:”
}]" 
+“\r\n”+“\r\n”);

这将直接在您插入的HTML中包含图像

您是否尝试过直接在
src
属性中使用
base64String
(即不作为单独的部分发送)?src=解决方案成功!非常感谢你!我真的很感激。如果其他人正在阅读,base64字符串必须以“data:image/jpeg;base64”作为前缀,因此在这种情况下,请使用imageDataURL字符串(在原始文章中尝试6)。
internal static async Task <HttpResponseMessage> UpdatePageAsync(GraphServiceClient client, OnenotePage page, string contentType, string content)
{
    HttpResponseMessage response = null;

    try
    {
        string requestUri = client.Users[ME].Onenote.Pages[page.Id].Content.Request().RequestUrl;

        List<OnenotePatchContentCommand> patchCommands = new List<OnenotePatchContentCommand>();

        HttpRequestMessage request = new HttpRequestMessage()
        {
            Method = new HttpMethod("PATCH"),
            RequestUri = new Uri(requestUri),
            Content = new StringContent(content, Encoding.UTF8, "application/json"),
        };

        request.Content.Headers.Remove("Content-Type");
        request.Content.Headers.TryAddWithoutValidation("Content-Type", contentType);

        // Adds the user's access token from the GraphServiceClient to the request.
        await client.AuthenticationProvider.AuthenticateRequestAsync(request);

        response = await client.HttpProvider.SendAsync(request);

        if (!response.IsSuccessStatusCode)
        {
            throw new Microsoft.Graph.ServiceException(
                new Error
                {
                    Code = response.StatusCode.ToString(),
                    Message = await response.Content.ReadAsStringAsync()
                });
        }
    }
    catch (Exception ex)
    {
        //TODO: Error handling
        Console.WriteLine(ex.ToString());
    }
    return response;
}
string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);  //attempt5
string imageDataURL = string.Format("data:image/jpeg;base64,{0}", base64String);  //attempt6

/* Construct message content */
StringBuilder sb = new StringBuilder();
sb.Append("--MyPartBoundary198374" + "\r\n");
sb.Append(@"Content-Disposition: form-data; name=""Commands"""  + "\r\n");
sb.Append("Content-Type: application/json" + "\r\n" + "\r\n");

sb.Append(
@"[{
    'target':'body',
    'action':'append',
    'position':'before',
    'content':'<img src=" + imageDataURL  + " width=""400"" height=""500""/>'
}]" 

+ "\r\n" + "\r\n");