我想在FTP服务器上上传文件。如何实现这个功能。
我尝试过许多库,例如 raccoons、SimpleFTPSample,但这些库已弃用的代码无法正常工作。
NSURL *url_upload = [NSURL URLWithString:@"ftp://username:password!@host:port/filename.wav"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url_upload];
[request setHTTPMethod:@"PUT"];
NSString *file = [[NSBundle mainBundle] pathForResource:@"1kb" ofType:@"png"];
NSURL *docsDirURL = [NSURL fileURLWithPath:filePath];
NSURLProtectionSpace * protectionSpace = [[NSURLProtectionSpace alloc] initWithHost:url_upload.host port:[url_upload.port integerValue] protocol:url_upload.scheme realm:nil authenticationMethod:nil];
NSURLCredential *cred = [NSURLCredential
credentialWithUser:@"username"
password:@"pass!"
persistence:NSURLCredentialPersistenceForSession];
NSURLCredentialStorage * cred_storage ;
[cred_storage setCredential:cred forProtectionSpace:protectionSpace];
NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfig.URLCredentialStorage = cred_storage;
NSURLSession *upLoadSession = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil];
NSURLSessionUploadTask *uploadTask = [upLoadSession uploadTaskWithRequest:request fromFile:docsDirURL completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (error != NULL)
{
NSLog(@"%@", error.localizedDescription);
}
else{
}
}];
[uploadTask resume];
Run Code Online (Sandbox Code Playgroud)
请建议我一些 swift 或 Objective C 实现来将文件上传到 FTP 服务器上。
您可以使用
https://github.com/amosavian/FileProvider
let returnCredential = URLCredential(user:<username>, password:<password>, persistence: .none)
let ftpFileProvider = FTPFileProvider(baseURL: URL(string:<host eg ftp:>, mode: .default, credential: returnCredential, cache: .none)!
ftpFileProvider.copyItem(localFile: <local_file_path>, to:<where on ftp>, completionHandler: { error in
if error != nil {
print(error)
return
}
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1303 次 |
| 最近记录: |