Get file size on FTP download

2019-03-02 06:56发布

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!

2条回答
萌系小妹纸
2楼-- · 2019-03-02 07:07

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.

查看更多
【Aperson】
3楼-- · 2019-03-02 07:07

To get the file size you just need:

case NSStreamEventOpenCompleted: {
      fileSize = [[self.networkStream propertyForKey:(id)kCFStreamPropertyFTPResourceSize] integerValue];}

By the way do you know how to get the modification date of the file in the ftp server??

查看更多
登录 后发表回答