Developer/iOS

[iOS] zip 파일 다운로드, 업로드

블로blow 2011. 6. 21. 15:05
728x90
//파일 다운로드

- (IBAction) downloadZipFile

{

    NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://api.do"]

                                              cachePolicy:NSURLRequestUseProtocolCachePolicy

                                          timeoutInterval:60.0];

    // create the connection with the request

    // and start loading the data

    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if (theConnection) {

        // Create the NSMutableData to hold the received data.

        // receivedData is an instance variable declared elsewhere.

        receivedData = [[NSMutableData data] retain];

    } else {

        // Inform the user that the connection failed.

    }

    

}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

{

    // This method is called when the server has determined that it

    // has enough information to create the NSURLResponse.

    

    // It can be called multiple times, for example in the case of a

    // redirect, so each time we reset the data.

    

    // receivedData is an instance variable declared elsewhere.

    [receivedData setLength:0];

}


- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

{

    // Append the new data to receivedData.

    // receivedData is an instance variable declared elsewhere.

    [receivedData appendData:data];

}


- (void)connection:(NSURLConnection *)connection

  didFailWithError:(NSError *)error

{

    // release the connection, and the data object

    [connection release];

    // receivedData is declared as a method instance elsewhere

    [receivedData release];

    

    // inform the user

    NSLog(@"Connection failed! Error - %@ %@",

          [error localizedDescription],

          [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);

}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection

{

    // do something with the data

    // receivedData is declared as a method instance elsewhere

    

    NSString *fileInfo= [NSString stringWithFormat:@"Succeeded! Received %d bytes of data",[receivedData length]];

    [self performSelectorOnMainThread:@selector(log:) withObject:fileInfo waitUntilDone:YES];


    

    NSArray *pa = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *filepath = [pa objectAtIndex:0];

    NSString *documentPath = [filepath stringByAppendingPathComponent:@"test.zip"];

    

    

    

    

    [receivedData writeToFile:documentPath atomically:YES];    

    

                

    

    // release the connection, and the data object

    [connection release];

    [receivedData release];

} 



//파일 업로드.,

-(IBAction)fileUpload

{

    NSArray *down_pa = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *down_filepath2 = [down_pa objectAtIndex:0];

    NSString *down_filePath = [down_filepath2 stringByAppendingPathComponent:@"test.zip"];

    

    NSData *imageData = [down_filePath dataUsingEncoding:NSASCIIStringEncoding];

    

    

    //NSData *imageData = UIImageJPEGRepresentation(self.imageView.image, 90);     

    

// setting up the URL to post to

NSString *urlString = @"http://api.do";   

    

// setting up the request object now

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];

[request setURL:[NSURL URLWithString:urlString]];

[request setHTTPMethod:@"POST"];     

    

NSString *boundary = [NSString stringWithString:@"----WebKitFormBoundaryF1kvH5cIG9tbI2Wk"];

    

NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; 

    [request addValue:contentType forHTTPHeaderField:@"Content-Type"];

    

    NSString *Accept_Encoding = [NSString stringWithFormat:@" gzip,deflate,sdch"]; 

    [request addValue:Accept_Encoding forHTTPHeaderField:@"Accept-Encoding"];

    

    NSString *Accept_Language = [NSString stringWithFormat:@" ko-KR,ko;q=0.8,en-US;q=0.6,en;q=0.4"]; 

    [request addValue:Accept_Language forHTTPHeaderField:@"Accept-Language"];

    

    NSString *Accept_Charset = [NSString stringWithFormat:@" windows-949,utf-8;q=0.7,*;q=0.3"]; 

    [request addValue:Accept_Charset forHTTPHeaderField:@"Accept-Charset"];

        

NSMutableData *body = [[NSMutableData data] init];

    

    

    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding] ];

       

    

    [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"upload\"; filename=\"test.zip\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding] ];

    

    [body appendData:[[NSString stringWithFormat:@"Content-Type: application/zip\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding] ];

    

    

    [body appendData:imageData]; // 실제 이미지 Data append 해줌

    

    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding] ];

    

        

[request setHTTPBody:body];

    

    

// now lets make the connection to the web

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

    

    NSLog(@"Return String = %@",returnString);

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Msg" message:@"Upload completed" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

[alertView show];

[alertView release];

    

} 
728x90