iOS SMB上传文件到服务器
最近公司有一个项目,要把销售数据上传到商场的服务器,商场给的文档包含SMB://192.168.1.100 我一看懵逼了,这是什么鬼,然后就各种百度,最终找到了类似的Demo
SMB类似于FTP协议,是通过共享然后对共享的文件进行操作
代码很简单(主要是上传,下载可以看看上面👆的demo)
- (TOSMBSession *)session {
if (!_session) {
_session = [[TOSMBSession alloc] initWithIPAddress:@"10.0.1.81"];
[_session setLoginCredentialsWithUserName:@"username" password:@"password"];
}
return _session;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSString *path = [NSString stringWithFormat:@"/sunkang/Desktop/name.text"];//服务器路径
NSString *txtPath = [[NSBundle mainBundle] pathForResource:@"sun_kang" ofType:@"rtf"];
NSData *data = [[NSData alloc]initWithContentsOfFile:txtPath];//需要上传的文件转data
TOSMBSessionUploadTask *uploadTask = [self.session uploadTaskForFileAtPath:path data:data progressHandler:nil completionHandler:^{
//ok
} failHandler:^(NSError *error) {
//no
}];
[uploadTask resume];
}