使用Microsoft Graph v1.0 api和C#,我能够更新(修补)现有的OneNote页面,但是在将图像数据写入multipart / form-data部分后,生成的图像高度和宽度是正确的,但是,图像不会在页面上呈现 - 除了空图像占位符。
所以问题是,OneNote对PATCH命令所期望的正确图像格式是什么?文档声明它必须是二进制图像数据'。不应该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;
}
此帖中列出的建议未解决此问题: Inserting image into existing OneNote page via REST api not working
试图解决这个问题超过一天。有人可以提供PATCH命令所需的正确图像数据格式。
谢谢, 罗兰
答案 0 :(得分:1)
将之前的评论移至答案以防其他人遇到此问题。
不是发送多个部分,而是组合尝试5&amp; 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中包含图像。