Send Multi Image with Retrofit - Android Studio

I am trying to send images to .net core server with retrofit. There is no problem sending a single image with Multipartbody.Part. okay. But I try List or Array , the image array doesn't go to the server.

I've been trying for days, but I can't reach a conclusion, please help me.


.NetCore API

public IActionResult AddPost([FromForm]string title, [FromForm]string description, [FromForm]string[] steps, [FromForm]string[] ingredients, [FromForm]IFormFile[] photos)
        {}

Debug note;

title: okey, data exist.

description: okey, data exist.

steps: okey, data exist.

ingredients: okey, data exist.

**photos: COUNT:0, DATA NOT EXIST.*****************


Retrofit method;

 @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>

Use of Method;

viewModel.sendPost(
                    title = postTitle.text.toString(),
                    description = postDescription.text.toString(),
                    steps = steps,
                    ingredients = ingredients,
                    photos = photos
                )

this method create Array

val photos = Array<MultipartBody.Part>(mediaList.size){MultipartBody.Part.createFormData("", "")}

mediaList.forEachIndexed { index, part ->
                    photos[index] = part
                }

getPostMedia() Method

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
    }

Upvotes: 2

Views: 259

Answers (1)

  • I solved the problem *

Solution, in getPostMedia() method.

postList.add(prepareFilePart("photos", post.postUri))

Upvotes: 1

Related Questions