Ole*_*leg 4 iphone xcode objective-c ios ios5
我从服务器下载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];
}
Run Code Online (Sandbox Code Playgroud)
在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
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1678 次 |
| 最近记录: |