我正在使用以下代码将图像上传到服务器。
NSData *imageData = [[[NSData alloc]initWithData:UIImageJPEGRepresentation(m_image.image, 100)]retain];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
//now lets create the body of the post
//set name here
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
if (imageData) {
NSLog(@"length %d",[imageData length]);
[body appendData:[NSData dataWithData:imageData]];
}
NSString *bodystring1 = [[NSString alloc]initWithData:body encoding:NSUTF8StringEncoding];
NSLog(@"body : %@",bodystring1);
输出:长度 3826304 正文:(空)
一旦我将图像附加到正文,它就会变为空。任何人都有任何想法。为什么会这样?