获取 FTP 下载的文件大小
我正在使用“SimpleFTPSample”项目进行测试apple 了解 ftp 在 iOS 上的工作原理。我在“GetController.m”中添加了两行来获取要下载的文件的大小。
-(void)_startReceive
{
.
.
.
// Open a CFFTPStream for the URL.
ftpStream = CFReadStreamCreateWithFTPURL(NULL, (CFURLRef) url);
CFReadStreamSetProperty(ftpStream, kCFStreamPropertyFTPFetchResourceInfo, kCFBooleanTrue); // Added: To get file size
.
.
.
}
- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode
{
.
.
.
switch (eventCode) {
case NSStreamEventOpenCompleted: {
// Added: Finally get filesize
fileSize = [[self.networkStream propertyForKey:(id)kCFStreamPropertyFTPResourceSize] integerValue];
[self _updateStatus:@"Opened connection"];
} break;
.
.
.
}
现在我得到了正确的文件大小(fileSize),但之后下载将无法开始。 “NSStreamEventHasBytesAvailable”情况将不会被处理。我怎样才能让它发挥作用?失败在哪里?我想显示下载状态的进度条 - 所以我之前需要完整的文件大小。
我希望你能帮我解决这个问题。 谢谢!
im testing around with the "SimpleFTPSample" project from apple to understand how ftp with iOS works. i have added two lines in the "GetController.m" to get the size of the file to download.
-(void)_startReceive
{
.
.
.
// Open a CFFTPStream for the URL.
ftpStream = CFReadStreamCreateWithFTPURL(NULL, (CFURLRef) url);
CFReadStreamSetProperty(ftpStream, kCFStreamPropertyFTPFetchResourceInfo, kCFBooleanTrue); // Added: To get file size
.
.
.
}
- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode
{
.
.
.
switch (eventCode) {
case NSStreamEventOpenCompleted: {
// Added: Finally get filesize
fileSize = [[self.networkStream propertyForKey:(id)kCFStreamPropertyFTPResourceSize] integerValue];
[self _updateStatus:@"Opened connection"];
} break;
.
.
.
}
now i get the correct file size (fileSize) but after that the download won't start. the "NSStreamEventHasBytesAvailable" case won't be handled. how can i get this to work? where's the failure? i want to show a progress bar for the download state- so i need the complete filesize before.
i hope you can help me out with this.
thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我非常确定,当您设置 kCFStreamPropertyFTPFetchResourceInfo 时,这意味着您只对该特定请求中的资源大小感兴趣。
您可能应该创建一个新的控制器,它是 GetController 的子类,将其命名为 StatController... 将 _startReceive 和流处理程序重写为您的新方法。
在开始异步下载之前,使用 StatController 获取资源大小。
I am pretty sure that when you set kCFStreamPropertyFTPFetchResourceInfo, that means your are only interested in the resources size in that particular request.
you should probably make a new controller that is a subclass of GetController, call it StatController... override _startReceive and the stream handler to your new methods.
use the StatController to get the resource size before you start the async download.
要获取文件大小,您只需要:
顺便问一下,您知道如何获取 ftp 服务器中文件的修改日期吗?
To get the file size you just need:
By the way do you know how to get the modification date of the file in the ftp server??