Get file size on FTP download

2.7k views Asked by At

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

There are 2 answers

0
Grady Player On

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.

1
Frade On

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??