Ktor 使用 FormData 上传 ByteArray

问题描述 投票:0回答:1

我从 KMP 项目中的相机获取 ByteArray 并尝试将其上传到服务器。

我试试这个

                        val response = client.post(url) {
                            setBody(
                                MultiPartFormDataContent(
                                    formData {
                                        if (byteArrays != null) {
                                            append("image", byteArrays, Headers.build {
                                                append(HttpHeaders.ContentType, "image/png")
                                                append(HttpHeaders.ContentDisposition, "filename=${name}")
                                            })
                                        }
                                    },
                                )
                            )
                            onUpload { bytesSentTotal, contentLength ->
                                println("Sent $bytesSentTotal bytes from $contentLength")
                            }
                        }

但是错误

"Required request part 'file' is not present"

kotlin kotlin-multiplatform ktor ktor-client
1个回答
0
投票

看起来您已指定“image”作为表单数据的键名称。也许尝试将其切换为“文件”。

© www.soinside.com 2019 - 2024. All rights reserved.