浏览 SDKs · iOS
平台
SDKsiOS

上传文件

使用 iOS SDK 独立上传文件。

复制

独立文件上传可用于头像、群头像、资料附件或业务文件,不从属于消息。

参数说明

参数说明
fullPathApp 可读取的绝对文件路径。
name上传文件名;传 nil 时使用路径末段。
cause业务约定的上传原因。
onProgress返回已保存、当前处理和总字节数。
onCompletion返回总字节数、远端 URL 与 putType。
[[OIMManager manager] uploadFile:fileURL.path
                            name:fileURL.lastPathComponent
                           cause:@"user-avatar"
                      onProgress:^(NSInteger saved, NSInteger current, NSInteger total) {
    [uploadStore updateCurrentBytes:current totalBytes:total];
}
                    onCompletion:^(NSInteger total, NSString *url, NSInteger putType) {
    [profileService saveAvatarURL:url];
}
                       onSuccess:nil
                       onFailure:^(NSInteger code, NSString *message) {
    NSLog(@"uploadFile failed: %ld %@", (long)code, message);
}];

返回结果

onCompletion 回调返回已上传资源的远端访问 URL、总字节数以及存储类型(putType)。整个上传操作最终状态以 onSuccess 或 onFailure 为准。该方法不会自动修改用户资料,也不会创建或发送消息;若资源随后用于聊天,应使用回调返回的 URL 创建对应类型的消息后再行发送。