SampleViewController.h
@interface SampleViewController : UIViewController <NSURLConnectionDelegate> {
NSMutableData *receivedData;//NSURLConnectionDelegate使用
}
@property (nonatomic, strong) NSMutableData *receivedData;
@end
與同步的做法,差別在於這邊使用
[NSURLConnection connectionWithRequest:request delegate:self];
來傳遞,透過delegate 來接收回傳的event
SampleViewController.m
@synthesize receivedData;
- (void)uploadFileDelegate:(NSString *)url fileData:(NSData *)paramData filePath:(NSString *)path{
self.receivedData=[[NSMutableData alloc] init];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
self.receivedData=[[NSMutableData alloc] init];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setTimeoutInterval:30];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"0xKhTmLbOuNdArY";//NSURLConnection is very sensitive to format.
// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];[request setValue:contentType forHTTPHeaderField: @"Content-Type"];
// post body
NSMutableData *body = [NSMutableData data];
// add params (all params are strings)
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", @"path"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n",path] dataUsingEncoding:NSUTF8StringEncoding]];
// add image data
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", @"file"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Type: image/jpeg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:paramData];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// set the content-length
NSString *postLength = [NSString stringWithFormat:@"%d", [body length]];[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
// set URL
[request setURL:[NSURL URLWithString:url]];
[NSURLConnection connectionWithRequest:request delegate:self];
//進入NSURLConnection Delegate
}
#pragma mark -
#pragma mark NSURLConnection Delegate
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
// store data
NSLog(@"didReceiveResponse");
[self.receivedData setLength:0]; //通常在這裡會先清空回傳值的暫存
}- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSLog(@"didReceiveData");
[self.receivedData appendData:data]; //可能多次收到多次回傳值,把新的回傳值加在原有回傳值後面
}- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
// 錯誤例外處理
NSLog(@"didFailWithError");
}
// disconnect
NSString *checkData=[NSString stringWithFormat:@"%@",[[NSString alloc] initWithData:self.receivedData encoding:NSUTF8StringEncoding]];
}