繁体   English   中英

NsmutableData在附加数据上获取空值

[英]Nsmutabledata getting null on append data

我正在使用以下代码将图像上传到服务器。

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正文:(空)

我将图片附加到正文后,它就会立即为null。 任何人任何想法。 为什么会这样呢?

尝试以下代码:

-(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"]);

}

让我知道有什么问题。

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

您不能使用字符串数据来处理图像数据。 由于这样做会破坏您的数据,因此将无法形成任何适当的结果。 因此,您需要以蛇形数据块上传字符串和图像。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM