class ArticleViewModel : ViewModel() {
val picture by lazy { MutableLiveData<Picture>() }
fun uploadSinglePicture(file: File) { val builder = MultipartBody.Builder().setType(MultipartBody.FORM) val requestFile: RequestBody = RequestBody.create(MediaType.parse("image/*"), file) builder.addFormDataPart("file", file.name, requestFile) val part = builder.build().part(0) viewModelScope.launch { val resultData = RetrofitClient.articleService.uploadSingle(part) picture.value = resultData.data } }
fun uploadSinglePicture(files: List<File>) { val builder = MultipartBody.Builder().setType(MultipartBody.FORM) for (file in files) { val requestFile: RequestBody = RequestBody.create(MediaType.parse("image/*"), file) builder.addFormDataPart("file", file.name, requestFile) } val parts = builder.build().parts() viewModelScope.launch { val resultData = RetrofitClient.articleService.uploadMulti(parts) picture.value = resultData.data } } }
|