在我的 iPhone 应用程序中,我在表格 View 中显示添加到文档目录的文件的信息,只要这些文件被添加。为此,我使用 DirectoryWatcher苹果在其中一个示例代码中提供的类。
下面是显示其用法的代码块:
- (void)viewDidLoad
{
// start monitoring the document directory…
self.aDirectoryWatcher = [DirectoryWatcher watchFolderWithPath:[self applicationDocumentsDirectory] delegate:self];
// scan for existing documents
[self directoryDidChange:self.aDirectoryWatcher];
}
- (void)directoryDidChange:(DirectoryWatcher *)folderWatcher
{
[self reconcileData];
}
表格 View 单元格中显示的信息之一是-文件大小,我获取如下:
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:[fileURL path] error:nil];
NSNumber * size = [fileAttributes objectForKey:NSFileSize];
问题是-
When I am trying to add a large file, such as a movie file, then as soon as transfer starts (copy or move operation) it invokes directoryDidChange: immediately. It did not wait unless the transfer is complete. So I always get size as 0.
对于小文件,例如图像,它工作正常。
现在我有两个问题:
有没有办法知道处于传输状态的文件的完整大小。例如。如果显示的消息是复制 30 MB 的 100 MB,我想获得 100 MB?
是否有 DirectoryWatcher 的替代方案,它仅在文件完全添加时通知?
请提出建议。
最佳答案
您当前正在查找文件系统,您应该查看下载请求的响应 header 。
例如,当您使用 NSURLConnection 下载文件时,您可以实现委托(delegate)方法 connection:didReceiveResponse: 并查看响应 header 。
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
NSLog(@"Expected content length: %lld", httpResponse.expectedContentLength);
}
}
要在完成时收到通知,您可以实现connectionDidFinishLoading:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// Notify download success
}
和
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
// Notify error
}
关于ios - 传输过程中识别文件的完整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20010944/