iOS   发布时间:2022-05-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了iphone – 如何将下载的数据保存到设备的文件系统大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我从服务器下载pdf文件: - (void)downloadSingleDocument:(NSURL *)url { [pdfData release]; pdfData = [[NSMutableData alloc] init]; NSMutableURLrequest *req = [NSMutableURLrequest requestWithURL:url];
我从服务器下载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
 }

大佬总结

以上是大佬教程为你收集整理的iphone – 如何将下载的数据保存到设备的文件系统全部内容,希望文章能够帮你解决iphone – 如何将下载的数据保存到设备的文件系统所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。