我有一种方法,可以通过FormData将图片从客户端发送到CDN。代码:
def uploadToCDN(formData: Multipart.FormData): Future[HttpResponse] = {
implicit val system = ActorSystem()
implicit val materializer = ActorMaterializer()
implicit val executionContext = system.dispatcher
Http().singleRequest(
HttpRequest(
method = HttpMethods.POST,
uri = "http://cdn.example.com",
entity = formData.toEntity(),
protocol = HttpProtocols.`HTTP/1.1`))
}
如何将“ secret_key”:“ 12345678” 添加到从客户端收到的FormData中?
答案 0 :(得分:1)
Multipart.FormData
基本上由其部分组成。要连接两个FormData
,您需要连接formdata部分并创建FormData
的新实例:
val newFormData =
Multipart.FormData(
Source.single(Multipart.FormData.BodyPart("secret_key", "12345678"))
.concat(originalFormData.parts)
)
另请参阅Multipart.FormData
中的Scaladocs。