我正在尝试将图像进行翻新发送到.net核心服务器。使用Multipartbody.Part发送单个图像没有问题。好的。但是我尝试使用List或Array,图像数组不会发送到服务器。
我已经尝试了好几天,但无法得出结论,请帮助我。
。NetCore API
public IActionResult AddPost([FromForm]string title, [FromForm]string description, [FromForm]string[] steps, [FromForm]string[] ingredients, [FromForm]IFormFile[] photos)
{}
调试说明;
标题:okey,数据存在。
描述:okey,数据存在。
步骤:好的,数据存在。
成分:好,数据存在。
**照片:COUNT:0,不存在数据。*****************
改造方法;
@Multipart
@POST("Post/addpost")
fun sendPost(@Part ("title") title: String,
@Part ("description") description: String,
@Part ("steps") steps: Array<String>,
@Part ("ingredients") ingredients: Array<String>,
@Part photos: Array<MultipartBody.Part> ): Call<String>
方法的使用;
viewModel.sendPost(
title = postTitle.text.toString(),
description = postDescription.text.toString(),
steps = steps,
ingredients = ingredients,
photos = photos
)
此方法创建数组
val photos = Array<MultipartBody.Part>(mediaList.size){MultipartBody.Part.createFormData("", "")}
mediaList.forEachIndexed { index, part ->
photos[index] = part
}
getPostMedia()方法
private fun getPostMedia(): MutableList<MultipartBody.Part> {
var postList = mutableListOf<MultipartBody.Part>()
PostList.instance!!.forEachIndexed { index, post ->
if (!post.isAddPost) {
postList.add(prepareFilePart("post[$index]", post.postUri))
}
}
return postList
}
解决方案,使用getPostMedia()方法。
postList.add(prepareFilePart("photos", post.postUri))