面对将文件上传到ftp服务器的问题

时间:2014-03-04 11:52:38

标签: ios iphone ftp core-foundation nsurlsession

当我尝试使用NSURLSession通过FTP上传文件时,我面临许可问题。但是,尝试使用Core Foundation框架文件时,不会上传文件,而是使用我在代码中提供的名称创建文件,甚至不会遇到任何权限问题。对于我都传递相同的凭据。如果有人想看代码,请告诉我我会上传它。我想添加暂停和恢复功能。所以我没有使用任何库,即使我想自己实现它,以便我可以了解。

1 个答案:

答案 0 :(得分:1)

为了将文件上传到FTP服务器,不能使用NSURLConnection,而是使用SCRFTPConnection。您需要包含CFNetwork框架。另外,下面是从Github下载SCRFTPRequest.h和SCRFTPRequest.m文件的链接

https://github.com/Hackmodford/SCRFTPRequest

从您从Github下载的上述zip文件中复制.h和.m文件,并将其添加到您的项目中。

// IN .H您的视图控制器的文件

导入“SCRFTPRequest.h”文件并添加代理

// IN .M您的视图控制器的文件

-(void)uploadFileUsingSCRFTPRequest{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *file_name=[NSString stringWithFormat:@"TEST.txt"];
    NSString *myPathDocs =  [documentsDirectory stringByAppendingPathComponent:file_name];

    SCRFTPRequest *ftpRequest = [[SCRFTPRequest alloc] initWithURL:[NSURL URLWithString:@"ftp://your_url/"] toUploadFile:myPathDocs];

    ftpRequest.username = @"username";
    ftpRequest.password = @"password";

    ftpRequest.delegate = self;

    [ftpRequest startRequest];

}

- (void)ftpRequestDidFinish:(SCRFTPRequest *)request {
    NSLog(@"Upload finished.");
}

- (void)ftpRequest:(SCRFTPRequest *)request didFailWithError:(NSError *)error {

    NSString *str=[NSString stringWithFormat:@"Upload failed: %@", [error localizedDescription]];
    NSLog(@"Upload failed: %@", [error localizedDescription]);

}