Nsmutabledata在追加数据时变为null

时间:2013-05-07 08:43:56

标签: iphone objective-c nsdata

我使用下面的代码将图片上传到服务器。

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 body:(null)

一旦我将图像附加到正文,它就会变为空。任何人都有任何想法。为什么会这样?

4 个答案:

答案 0 :(得分:1)

尝试以下代码:

-(void) uploadImage
{
NSData *imageData = UIImagePNGRepresentation([dic objectForKey:@"image"]);
// setting up the URL to post to
NSString *urlString =[NSString stringWithFormat:@"%@YOURURL?User_ID=%@",BaseUrl,       [dicobjectForKey:@"User_ID"]];

// setting up the request object now

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
[request setTimeoutInterval:10];

/*
add some header info now
we always need a boundary when we post a file
also we need to set the content type

You might want to generate a random boundary.. this is just the same
as my output from wireshark on a valid html post
*/

NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

/*
now lets create the body of the post
*/

NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data;   name=\"profile_photo\"; filename=\"ipodfile.png\"\r\n"]   dataUsingEncoding:NSUTF8StringEncoding]];
// where profile_photo is the key value or parameter value on server. must be confirm

[body appendData:[[NSString stringWithString:@"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]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];

// now lets make the connection to the web
NSData *returnData = [ NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil ]; // send data to the web service
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSASCIIStringEncoding];
NSString *trimmedString = [returnString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
NSString *trimmedString1 = [trimmedString stringByReplacingOccurrencesOfString:@"\t" withString:@""];
trimmedString1 = [trimmedString1 stringByReplacingOccurrencesOfString:@"\r" withString:@""];

NSMutableArray *dic0 = [trimmedString1 JSONValue];
NSMutableDictionary *dic1 = [dic0 objectAtIndex:0];
NSLog(@"Profile photo status : %@",[dic1 valueForKey:@"id"]);

}

让我知道任何问题。

答案 1 :(得分:1)

这里的body包含边界值,内容信息和Image作为NSData。因此将此转换为字符串将仅返回null,因为内部SDK将无法识别正文中NSData格式的图像。这就是为什么此行返回null

答案 2 :(得分:0)

您无法使用字符串数据处理图像数据。由于您的数据会因此而损坏,并且无法形成任何适当的结果。因此,您需要在蛇形数据块中上传字符串和图像。

答案 3 :(得分:0)

我看不出,那个身体是零。你记录bodystring1,而不是身体。 可能图像数据包含无效的UTF8代码。 -initWithData:encoding:将拒绝该数据并返回nil。

将图像数据直接转换为字符串并不是一个好主意。使用Base64编码或类似的东西。