簡體   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