1

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

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

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

4

4 回答 4

1

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

于 2014-08-08T05:18:12.733 回答
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"]);

}

让我知道任何问题。

于 2013-05-07T08:54:15.623 回答
0

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

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

于 2013-05-07T08:51:35.923 回答
0

您不能使用字符串数据操作图像数据。因为这样做会损坏您的数据,并且无法形成任何适当的结果作为回报。因此,您需要以蛇形数据块的形式上传字符串和图像。

于 2013-05-07T08:50:33.513 回答