我的项目中有PDF文件。我想给功能下载该文件到移动目录并加载其他应用程序。如何从App目录下载和加载文件到本地
回答
NSError *error;
NSFileManager *fileManager = [NSFileManager defaultManager];
//NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"/RTOFiles"];;
if (![[NSFileManager defaultManager] fileExistsAtPath:documentsDir])
[[NSFileManager defaultManager] createDirectoryAtPath:documentsDir withIntermediateDirectories:NO attributes:nil error:&error];
NSString *pdfName = @"form_1.pdf";
NSString *docPdfFilePath = [documentsDir stringByAppendingPathComponent: pdfName];
//Using NSFileManager we can perform many file system operations.
BOOL success = [fileManager fileExistsAtPath: docPdfFilePath];
if (!success) {
NSString *samplePdfFile = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: pdfName];
success = [fileManager copyItemAtPath: samplePdfFile toPath: docPdfFilePath error: &error];
if (!success)
// NSAssert1(0, @"Failed to copy file ‘%@’.", [error localizedDescription]);
NSLog(@"Failed to copy %@ file, error %@", pdfName, [error localizedDescription]);
else {
NSLog(@"%@ File copied to %@", pdfName,documentsDir);
}
}
else{
NSLog(@"File already Exists !");
}
这将使用双重记忆体,因为它在应用程序包中以及docs目录中。您可以直接使用应用程序包中的文件。我正在写答案转介它。 – Lion
你可以直接使用你的形象,如果它是在应用程序bunldle一样,
UIImage *img = [UIImage imageNamed:@"Amy.png"];
如果你想在webview
从应用程序捆绑加载pdf
那么你可以这样做,
NSString *path = [[NSBundle mainBundle] pathForResource:@"yourPdfName" ofType:@"pdf"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
总之,如果你有应用程序包中的文件可用,那么你不需要将它复制到documentDirectory
,因为它会使用双倍的内存,你可以直接从捆绑使用它。
如果用户不想从App打开文件,那么他们也可以通过文档目录打开它,所以我使用了这个。感谢您的支持 ! –
用户可以直接从应用程序包打开,如果它在应用程序包中,则无需在文档目录中复制! – Lion
- 1. 将文件夹从GCS下载到本地目录
- 2. 从服务器下载CSV文件到本地目录
- 3. 下载到本地根本目录
- 4. 如何将下载的文件对象从S3存储到本地目录
- 5. 如何使用C#将文件从ftp下载到本地目录?
- 6. gettext - 加载本地目录
- 7. 从本地目录加载json
- 8. 使用FTP.exe和mget从FTP目录下载文件和目录
- 9. 使用文件名从本地目录加载多个图像
- 10. Swift3:如何从文档目录加载html文件到WKWebview
- 11. 从项目目录加载XML文件
- 12. 下载CSV文件到本地下载的文件夹
- 13. 从powershell 2.0下载文件到目录脚本
- 14. 如何从文档目录加载CCSpriteBatchNode?
- 15. 如何使用google oauth将所有文件从谷歌存储桶目录下载到本地目录
- 16. 从插件目录加载文件
- 17. 从目录下载随机文件
- 18. 从虚拟目录下载文件
- 19. 从网页目录下载文件
- 20. 从目录列表中下载文件
- 21. 从固定目录下载PHP文件
- 22. 从网络目录下载文件
- 23. 如何使用命令从linux下载文件夹到本地
- 24. 如何从EC2实例文件下载到本地计算机
- 25. 如何从Github下载文本文件?
- 26. 如何将excel文件保存在我的本地目录下用php下载
- 27. 如何从本地目录中以C++加载图像
- 28. 如何链接要从本地目录加载的库?
- 29. 如何从本地子目录加载宝石?
- 30. 从Android应用下载的文件不可见下载目录
没有得到你正确的!你的pdf在你的项目中意味着什么?在你的应用程序包?如果它在你的应用程序包中,那么你为什么要下载它? – Lion
我已将所有PDF与我的应用程序相连,它对用户不可见。所以只是想将这些PDF复制到用户本地文件夹,所以他们可以很容易地将它用于任何应用程序 –
iPhone没有本地文件夹,您会在哪里复制PDF?您可以通过UIActivityViewController查看共享。 –