我正在使用iOS应用程序中的OneDrive iOS SDK创建OneNote页面。这是我为多个图像发布请求所做的代码。
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中创建包含图像的页面,但我无法在页面上看到图像。请帮帮我。无法找出请求中的问题。我正在创建请求有什么问题吗?
答案 0 :(得分:1)
如果您收到“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];
}];
如果你有这些标题:
Content-Type: multipart/form-data; boundary=0FA204EB-20EA-4E56-99EC-5EF56D600E02
这个身体应该有效。
--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--
.
答案 1 :(得分:0)
这是注释中的一个或多个图像。
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];
}