-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
if(data != nil){
//open output stream
NSOutputStream *stream=[[NSOutputStream alloc] initToFileAtPath:_filePath append:YES];
[stream open];
NSString *str=(NSString *)data;
//write to file
NSUInteger left = [str length];
NSUInteger bytesWritten = 0;
do {
bytesWritten = [stream write:[data bytes] maxLength:left];
downloadedData = downloadedData + bytesWritten;
if (-1 == bytesWritten) break;
left -= bytesWritten;
} while (left > 0);
if (left) {
NSLog(@"stream error: %@", [stream streamError]);
[self handleForCurreptedDownloading];
}
[stream close];
}
else{
NSLog(@"data nil");
}
}
-(void)connection:(NSURLConnection *)connection