在将文档下载到文档目录后,我正在尝试检查文档的文件大小是否正确。我不确定在我的网络服务帮助文件中的哪里检查它?或者最好的方法。
我能够在开始下载之前获得预期的文件长度。我可以得到每次突发发送的数据长度。我正在考虑每次在 didReceiveData 中添加这些突发数字,然后与预期长度进行比较,但不确定如何正确添加。
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
filesize1 = [[NSNumber numberWithLong: [response expectedContentLength]] retain];
NSLog(@"%@", filesize1);
if(requestType == DF_WS_REQUEST_GET_PDF_DATA)
{
[[NSFileManager defaultManager] createFileAtPath:self.filename contents:nil attributes:nil];
self.file = [[NSFileHandle fileHandleForUpdatingAtPath:self.filename] retain];// file is in .h
if (self.file)
{
[self.file seekToEndOfFile];
}
}
else
{
[webData setLength: 0];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
resourceLength = [NSNumber numberWithUnsignedInteger:[data length]];
NSLog(@"resource length: %d", [resourceLength intValue]);
if(requestType == DF_WS_REQUEST_GET_PDF_DATA)
{
if (self.file)
{
[self.file seekToEndOfFile];
}
[self.file writeData:data];
}
else
[webData appendData: data];
}
- (void)connectionDidFinishingLoading:(NSURLConnection *)connection {
URL = filePreviewViewController.filePath;
NSError *AttributesError = nil;
NSDictionary *FileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:URL error:&AttributesError];
NSNumber *FileSizeNumber = [FileAttributes objectForKey:NSFileSize];
long FileSize = [FileSizeNumber longValue];
NSLog(@"File: %@, Size: %ld", URL, FileSize);
}
任何帮助或想法将不胜感激。谢谢。