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

PATCH existing OneNote Page by appending binary image content

使用 Microsoft Graph v1.0 api 和 C#,我能够更新(修补)现有的 OneNote 页面,但是在将图像数据写入 multipart/form-data 部分后,生成的图像高度和宽度正确,但图像未呈现在页面上 -- 除了空图像占位符。

那么问题来了,对于 PATCH 命令,OneNote 期望的正确图像格式是什么?文档声明它必须是 'binary image data'。 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

...

/* 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=""name:image1"" width=""400"" height=""500""/>'
}]" 

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

sb.Append("--MyPartBoundary198374" + "\r\n");
sb.Append(@"Content-Disposition: form-data; name=""image1""" + "\r\n");
sb.Append("Content-Type: image/jpeg" +  "\r\n\r\n" );
sb.Append([see formats above] + "\r\n");
sb.Append("--MyPartBoundary198374--" );

string content = sb.ToString();
string contentType = "multipart/form-data; boundary=MyPartBoundary198374";
var result = await OneNoteService.UpdatePageAsync(client, page, contentType, content);

...

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;
}

post中列出的建议没有解决问题: Inserting image into existing OneNote page via REST api not working

一天多来一直在努力解决这个问题。有人可以提供 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");

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