I am creating OneNote page using OneDrive iOS SDK from my iOS application. This is the code i did for post request for multiple image.
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];
}
I am successfully able to create page with images in OneNote but i am unable to see images on page. Please help me on this. Unable to find out what is the problem in request. Is there any problem is request i am creating ?