Necesito descargar un archivo grande (es decir> 40MB) a mi aplicación del servidor, este archivo será ZIP o PDF. Lo logré utilizando NSURLConnection, que funciona bien si el archivo es más pequeño, de lo contrario, descarga una parte del relleno y la ejecución se detiene. por ejemplo, traté de descargar un archivo PDF de 36 MB, pero solo descargué 16 MB. pls me ayuda a saber la razón? ¿Como arreglarlo?NSURLConnection descargar archivo grande (> 40MB)
FYI: Implementación del archivo
#import "ZipHandlerV1ViewController.h"
@implementation ZipHandlerV1ViewController
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (void)viewDidLoad
{
UIView *mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 400, 400)];
[mainView setBackgroundColor:[UIColor darkGrayColor]];
UIButton *downloadButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[downloadButton setFrame:CGRectMake(50, 50, 150, 50)];
[downloadButton setTitle:@"Download File" forState:UIControlStateNormal];
[downloadButton addTarget:self action:@selector(downloadFileFromURL:) forControlEvents:UIControlEventTouchUpInside];
[mainView addSubview:downloadButton];
[self setRequestURL:@"http://www.mobileveda.com/r_d/mcps/optimized_av_allpdf.pdf"];
[self.view addSubview:mainView];
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(void) setRequestURL:(NSString*) requestURL {
url = requestURL;
}
-(void) downloadFileFromURL:(id) sender {
NSURL *reqURL = [NSURL URLWithString:url];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:reqURL];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
webData = [[NSMutableData data] retain];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error !" message:@"Error has occured, please verify internet connection" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[webData setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[webData appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *fileName = [[[NSURL URLWithString:url] path] lastPathComponent];
NSArray *pathArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *folder = [pathArr objectAtIndex:0];
NSString *filePath = [folder stringByAppendingPathComponent:fileName];
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
NSError *writeError = nil;
[webData writeToURL: fileURL options:0 error:&writeError];
if(writeError) {
NSLog(@" Error in writing file %@' : \n %@ ", filePath , writeError);
return;
}
NSLog(@"%@",fileURL);
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error !" message:@"Error has occured, please verify internet connection.." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
[alert release];
}
@end
del encabezado del archivo:
#import <UIKit/UIKit.h>
@interface ZipHandlerV1ViewController : UIViewController {
NSMutableData *webData;
NSString *url;
}
-(void) setRequestURL:(NSString*) requestURL;
@end
Gracias,
Actualizado: Esto sucede debido a mi archivo descargable estaba en alojamiento compartido que tiene las limitaciones de descarga. Después de que moví ese archivo al servidor dedicado que funciona bien. y también traté de descargar algunos otros archivos, como videos de otros sitios, que también funcionan bien. Por lo tanto, si usted tiene un problema como descarga parcial, no sólo se pegan con el código, compruebe el servidor demasiado
incluso después de hacer los cambios que mencionó, descarga solo 6.4MB de un archivo de 39MB, ¿Por qué sucede esto? ¿hay alguna limitación de memoria? .., lo probé tanto en el simulador como en el dispositivo iPad. – Jayabal
Esto debe sobrescribir el archivo cada vez que se invoque a fromReceiveData. –
@Vova, tiene razón, mi ejemplo era solo una pista y no cubría completamente las necesidades. Cambié eso y ahora se necesita un cuidado extra para todas las situaciones posibles. – Till