如何在单个 OneNote 页面上传多张图片?

How to upload multiple image in single OneNote page?

我正在使用 iOS 应用程序中的 OneDrive iOS SDK 创建 OneNote 页面。这是我为 post 请求多张图片所做的代码。

NSMutableArray *selecedFileNames = [[NSMutableArray alloc] init];
NSString *date = [self formatDate];
NSString *start = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n"
                   "<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en-us\">\r\n"
                   "<head>\r\n"
                   "<title>Created By</title>\r\n"
                   "<meta name=\"created\" content=\"%@\" />\r\n"
                   "</head>\r\n"
                   "<body>\r\n"
                   "<p>This is <b>Photo</b> <i>Created</i> note.</p>\r\n",date];

for (int i = 0; i<self.selectedImages.count; i++) {

    self.imgObject = (ImageObjects *)[self.selectedImages objectAtIndex:i];

    FileModel *fileModel = [FileModel new];
    fileModel.fileData = self.imgObject.image_data;
    fileModel.fileName =  self.imgObject.imageName;
    fileModel.fileType = FileTypeImage;

    self.filePath = fileModel.getFileName;

    NSString *trimmedString = [self.filePath  stringByReplacingOccurrencesOfString:@" " withString:[NSString stringWithFormat:@"%d",i]];
    self.filePath = [trimmedString  stringByReplacingOccurrencesOfString:@":" withString:@""];

    NSString *middle = [NSString stringWithFormat:@"<img src=\"%@\" alt=\"image\" width=\"500\">\r\n",self.filePath];

    [selecedFileNames addObject:self.filePath];
    start = [start stringByAppendingString:middle];

}

start = [start stringByAppendingString:@"</body>\r\n"
         "</html>\r\n"];

NSString *boundary = [self generateBoundaryString];

NSString *pagesEndpoint = [NSString stringWithFormat:@"sections/%@/pages", selectedSectionId];
NSString *fullEndpoint = [serviceRootUrl stringByAppendingString:pagesEndpoint];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:[NSURL URLWithString:fullEndpoint]];
[request setHTTPMethod:@"POST"];

// set content type
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];

NSMutableData *body = [NSMutableData data];


[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"Presentation\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type:application/xhtml+xml\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", start] dataUsingEncoding:NSUTF8StringEncoding]];

for (int i=0; i< self.selectedImages.count; i++) {

    self.imgObject = (ImageObjects *)[self.selectedImages objectAtIndex:i];

    FileModel *fileModel = [FileModel new];
    fileModel.fileData = self.imgObject.image_data;
    fileModel.fileName =  self.imgObject.imageName;
    fileModel.fileType = FileTypeImage;

    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\" \r\n",[selecedFileNames objectAtIndex:i]] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"\r\n--" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:fileModel.fileData]];
    [body appendData:[@"--\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

}

[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n.", boundary] dataUsingEncoding:NSUTF8StringEncoding]];


NSString *finalBody =[[NSString alloc] initWithData:body encoding:NSASCIIStringEncoding];

// set request body
[request setHTTPBody:body];

if (self.client)
{
    // Send the HTTP request.
    [self sendRequest:request];
}

我可以在 OneNote 中成功创建带有图像的页面,但我无法在页面上看到图像。请帮我解决这个问题。无法找出请求中的问题所在。我创建的请求有什么问题吗?

如果您收到“413”响应代码,则表示您超出了发送到 API 的请求的大小。这取决于您发送的图像的总大小,而不是图像的数量。

请注意,通过 Base64 字符串发送图像会导致其大小增加 33%(这就是示例所做的)。您可以通过发送多部分请求来避免这 33% 的命中率。此外,您可以使用 JPEG 等文件格式显着减小图像尺寸(看起来您使用的是 PNG,因此您可能仍需要采取额外步骤来减小图像尺寸)。

我们的 iOS 示例在函数 createPageWithImage

中正是这样做的
NSString *attachmentPartName = @"pngattachment1";
NSString *date = dateInISO8601Format();
UIImage *logo = [UIImage imageNamed:@"Logo"];

NSString *simpleHtml = [NSString stringWithFormat:
                        @"<html>"
                        "<head>"
                        "<title>A simple page with an image from iOS</title>"
                        "<meta name=\"created\" content=\"%@\" />"
                        "</head>"
                        "<body>"
                        "<h1>This is a page with an image on it</h1>"
                        "<img src=\"name:%@\" alt=\"A beautiful logo\" width=\"%.0f\" height=\"%.0f\" />"
                        "</body>"
                        "</html>", date, attachmentPartName, [logo size].width, [logo size].height];

NSData *presentation = [simpleHtml dataUsingEncoding:NSUTF8StringEncoding];
NSData *image1 = UIImageJPEGRepresentation(logo, 1.0);
NSString *endpointToRequest = [ONSCPSCreateExamples getPagesEndpointUrlWithSectionName:sectionName];
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:endpointToRequest parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
    [formData
     appendPartWithHeaders:@{
                             @"Content-Disposition" : @"form-data; name=\"Presentation\"",
                             @"Content-Type" : @"text/html"}
     body:presentation];
    [formData
     appendPartWithHeaders:@{
                             @"Content-Disposition" : [NSString stringWithFormat:@"form-data; name=\"%@\"", attachmentPartName],
                             @"Content-Type" : @"image/jpeg"}
     body:image1];
}];

如果你有这些 headers:

Content-Type: multipart/form-data; boundary=0FA204EB-20EA-4E56-99EC-5EF56D600E02

还有这个 body,它应该可以工作。

--0FA204EB-20EA-4E56-99EC-5EF56D600E02
Content-Disposition: form-data; name="Presentation"
Content-Type: text/html
<!DOCTYPE html>
<html><head><title>Created By Photo</title>
<meta name="created" content="2017-02-01T11:34:11+05:30" /></head><body><p>This is some <b>Photo</b> <i>Created</i> note.</p></body><h1>This is a note with image.</h1>
<img src="name:2017-02-01060411+0000.JPG" alt="image" ></body></html>
--0FA204EB-20EA-4E56-99EC-5EF56D600E02
Content-Disposition: form-data; name="2017-02-01060411+0000.JPG"
Content-Type: image/jpeg
IMAGE
--0FA204EB-20EA-4E56-99EC-5EF56D600E02--
.

这是注释中的一张或多张图片。

 NSMutableArray *selecedFileNames = [[NSMutableArray alloc] init];
 NSString *date = [self formatDate];

 NSString *pagesEndpoint = [NSString stringWithFormat:@"sections/%@/pages", selectedSectionId];
 NSString *fullEndpoint = [serviceRootUrl stringByAppendingString:pagesEndpoint];

NSString *start = [NSString stringWithFormat:@"<html>"
                   "<head>"
                   "<title>Created By Photo</title>"
                   "<meta name=\"created\" content=\"%@\" />"
                   "</head>"
                   "<body>"
                   "<h1>This is a page with an image on it</h1>",date];

for (int i = 0; i<self.selectedImages.count; i++) {

FileModel *fileModel = [FileModel new];
fileModel.fileData = self.imgObject.image_data;
fileModel.fileName =  self.imgObject.imageName;
fileModel.fileType = FileTypeImage;

 self.filePath = fileModel.getFileName;
 UIImage *img = [UIImage imageWithData:fileModel.fileData];
 NSString *trimmedString = [self.filePath  stringByReplacingOccurrencesOfString:@" " withString:[NSString stringWithFormat:@"%d",i]];
 self.filePath = [trimmedString  stringByReplacingOccurrencesOfString:@":" withString:@""];

 NSString *middle = [NSString stringWithFormat:@"<h1>This is image</h1>"
                     "<img src=\"name:%@\" alt=\"A beautiful image\" width=\"%.0f\" height=\"%.0f\" />\r\n",self.filePath,[img size].width, [img size].height];

 [selecedFileNames addObject:self.filePath];
 start = [start stringByAppendingString:middle];

 }

 start = [start stringByAppendingString:@"</body>\r\n"
 "</html>\r\n"];


 NSData *presentation = [start dataUsingEncoding:NSUTF8StringEncoding];

 NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:fullEndpoint parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {


  [formData appendPartWithHeaders:@{
                              @"Content-Disposition" : @"form-data;               name=\"Presentation\"",
                              @"Content-Type" : @"text/html"}
      body:presentation];


 for (int i=0; i< self.selectedImages.count; i++) {

 self.imgObject = (ImageObjects *)[self.selectedImages objectAtIndex:i];

 FileModel *fileModel = [FileModel new];
 fileModel.fileData = self.imgObject.image_data;
 fileModel.fileName =  self.imgObject.imageName;
 fileModel.fileType = FileTypeImage;


     [formData
      appendPartWithHeaders:@{
                              @"Content-Disposition" : [NSString stringWithFormat:@"form-data; name=\"%@\"", [selecedFileNames objectAtIndex:i]],
                              @"Content-Type" : @"image/jpeg"}
      body:fileModel.fileData];

 }

 } error:nil];

 if (self.client)
 {
 // Send the HTTP request.
 [self sendRequest:request];
 }