iphone – 如何将下载的数据保存到设备的文件系统
发布时间:2020-12-14 17:49:30 所属栏目:百科 来源:网络整理
导读:我从服务器下载pdf文件: - (void)downloadSingleDocument:(NSURL *)url{ [pdfData release]; pdfData = [[NSMutableData alloc] init]; NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url]; [req addValue:@"Basic **************=" for
我从服务器下载pdf文件:
- (void)downloadSingleDocument:(NSURL *)url { [pdfData release]; pdfData = [[NSMutableData alloc] init]; NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url]; [req addValue:@"Basic **************=" forHTTPHeaderField:@"Authorization"]; downloadConnection = [[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:YES]; } - (void)connection:(NSURLConnection *)conn didReceiveData:(NSData *)data { NSLog(@"Connection did receive data"); [pdfData appendData:data]; } 在connectionDidFinishLoading上,我想将下载的pdf文件保存到Documents目录中的文件系统,文件名与服务器上的文件名相同. 解决方法
如果您有大文件,请使用此方法:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse*)response { filepath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) objectAtIndex:0] stringByAppendingPathComponent:save_name]; [[NSFileManager defaultManager] createFileAtPath:filepath contents:nil attributes:nil]; file = [[NSFileHandle fileHandleForUpdatingAtPath:filepath] retain];// Here file is object of NSFileHandle and its declare in .h File [file seekToEndOfFile]; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [file seekToEndOfFile]; [file writeData:data]; } - (void)connectionDidFinishLoading:(NSURLConnection*)connection { [file closeFile]; // After you download your data. you can copy your data from here to filesystem. and remove from here } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |