我的代码
NSString *urlString = [NSString stringWithFormat:@"%@%@", urlHeader, urlBody];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:0 timeoutInterval:TIMEOUT_INTERVAL];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"--Kqcekgk5QEa0NR71B";
NSMutableData *body = [NSMutableData data];
NSMutableData *imageBody = [NSMutableData data];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n%@", BOARD_ID, boardType] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n%d", BOARD_SEQ, [boardSeq intValue]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
for(int i=0; i<[imagesArr count]; i++) {
if([[imagesArr objectAtIndex:i] isMemberOfClass:[UIImage class]]) {
NSData *imageData = UIImageJPEGRepresentation([imagesArr objectAtIndex:i] , 90);
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"FILE_UPLOAD%d\"; filename=\"IMG_IPHONE%d.jpg\"\r\n\r\n", i+1, i+1] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type:application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
}
}
[body appendData:imageBody];
发送到服务器成功但图像文件类型为文本/计划我使用图像 .png
为什么图像文件损坏?