2017-04-13 76 views
1

我从iOS应用程序上传图像到SFTP服务器。为此,我使用NMSSH框架。现在我正在异步上传图像,下面是我的代码。我想批量上传图片。但我不知道我该怎么做。请建议。如何批量上传图像到SFTP服务器

-(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]; 

     } 

    }); 
} 

回答

0

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

+0

我知道HTTPS的多部分作品。它适用于SFTP吗? – reetu

相关问题