iphone – 如何将下载的数据保存到设备的文件系统

iphone – 如何将下载的数据保存到设备的文件系统,第1张

概述我从服务器下载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 – 如何将下载的数据保存到设备的文件系统所遇到的程序开发问题。

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

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1019058.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-23
下一篇2022-05-23

发表评论

登录后才能评论

评论列表(0条)

    保存