How to upload images to SFTP server in batches

时间:2017-04-13 14:56:12

标签: objective-c file-upload sftp batch-processing nmssh

I am uploading images to SFTP server from my iOS application. For this I am using NMSSH framework. Now I am uploading the images asynchronously and below is my code. I want to upload images in batches. But I don't have any idea how should I do it.Please suggest.

    -(void)connect{
   dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        //Initiate session to SFTP server
        NMSSHSession *session = [NMSSHSession connectToHost:@"host"
                                               withUsername:@"user"];
        if (!session.connected) {
            dispatch_async(dispatch_get_main_queue(), ^{
                NSLog(@"Connection Error");
            });

            return;
        }

        //Authenticate Session
        NSString *privateKey = [[NSBundle mainBundle] pathForResource:@"privatekey" ofType:nil];
        [session authenticateByPublicKey:nil
                              privateKey:privateKey
                             andPassword:nil];

        if (!session.isAuthorized) {
            NSArray *authTypes = [session supportedAuthenticationMethods];
            NSLog(@"authTypes is %@",authTypes);
            dispatch_async(dispatch_get_main_queue(), ^{
                //Update UI with error
                //Code for UI
            });
        }else{
            //Initiate SFTP Object with the session
            NMSFTP *sftp = [[NMSFTP alloc] initWithSession:session];
            [sftp connect];

            //Check SFTP contents
            NSArray *contentsOfDirectoryAtPath = [sftp contentsOfDirectoryAtPath:@"/"];
            NSLog(@"contentsOfDirectoryAtPath %@ ",contentsOfDirectoryAtPath);

            //Check if the directory exist in SFTP else Create
            if (![sftp directoryExistsAtPath:@"/mobileImages/siteid_123458"]) {
                [sftp createDirectoryAtPath:@"/mobileImages/siteid_123458"];
            }

            //Upload images to SFTP from Mobile
            for (uint i=0 ; i<photoDATA.count; i++) {
                [sftp writeContents:photoDATA[i] toFileAtPath:[NSString stringWithFormat:@"/mobileImages/siteid_123458/%@.jpg",[self getCurrentDate]] progress:^BOOL(NSUInteger sent) {
                     NSLog(@"%d Bytes sent",(int)sent);
                    //Code for file upload progress
                    return YES;
                }];

            }

            [session disconnect];

        }

    });
}

1 个答案:

答案 0 :(得分:0)

您可以使用多部分请求在服务器上上传图像。多部件要求重量轻很轻。