我是iPhone新手,在我的應用程序中使用XML,我需要將一個圖像發送到服務器的一個特定文件夾。出於同樣的我使用的代碼:未將對象引用設置爲對象的實例在XML中
NSString *filename = @"Image";
NSData *imageData = UIImageJPEGRepresentation([imageView image], 90);
NSString *urlString = [NSString stringWithFormat:@"http://111.111.11.1/webservices.asmx/PutImage"];
//網址是:http://111.111.11.1/webservices.asmx/PutImage?ImgIn=image.jpg
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = [NSString stringWithString:@"--------1473780983"];
NSString *contentType = [NSString stringWithFormat:@"application/x-www-form-urlencoded;boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"images\"; filename=\"%@.jpg\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
[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]];
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"\n\n\nreturnString : %@",returnString);
我得到的消息,如:在返回字符串「未設置爲一個對象的實例對象引用」 。任何人都可以指導我解決一些問題嗎?
your returnData is empty – sicKo
@sicKo:你能幫我解釋一下原因嗎? – Sarah