1

I need to upload multiple images to oneNote through "oneNote API", but I don't know how to write binary to code.

Here's the code in my code:

var client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", await Auth.GetAuthToken(provider));

string imagePartName = "imageBlock1";
StringBuilder simpleHtml = new StringBuilder();
simpleHtml.Append("<html lang=\"zh-CN\">\n");
simpleHtml.Append("<head>\n");
simpleHtml.Append("<title>" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "</title>\n");
simpleHtml.Append("<meta name=\"created\" content=\"" + DateTime.Now.ToString("o") + "\" />\n");
simpleHtml.Append("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" />\n");
simpleHtml.Append("</head>\n");
simpleHtml.Append("<body data-absolute-enabled=\"true\" style=\"font-family:Microsoft YaHei;font-size:11pt\">\n");
simpleHtml.Append("<img src=\"name:"+ imagePartName + "\" alt=\"a cool image\" width=\"500\"/>");
simpleHtml.Append("</body>\n");
simpleHtml.Append("</html>");

var createMessage = new HttpRequestMessage(HttpMethod.Post, apiRoute + "/pages")
{
    Content = new MultipartFormDataContent
    {
        {
            new StringContent(simpleHtml.ToString(), Encoding.UTF8, "text/html"), "Presentation"
        }, //Here is the HTML data

        //How to add "binary" data here
    }
};

response = await client.SendAsync(createMessage);

Looking forward to everyone's reply!

1 Answer 1

1

If you want to use MultipartFormDataContent you can convert your binary data to Base64 string (example). Drawback of this is the amount of characters you need to transfer.

There're several methods in MultipartFormDataContent that concerns 'stream'. It would be worth to look into those.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.