我有一个下一个表格:
<form action="/upload" method="POST" enctype="multipart/form-data">
<input type="text" name="name">
<input type="file" name="file">
<input type="submit" value="Upload image">
</form>
我想发送请求 withname
和 withfile
我用spray-client
这个,当我只发送文件时,这个工作正常:
val file = "my-image.png"
val bis = new BufferedInputStream(new FileInputStream(file))
val bArray = Stream.continually(bis.read).takeWhile(-1 !=).map(_.toByte).toArray
val url = "http://example.com/upload"
val httpData = HttpData(bArray)
val httpEntity = HttpEntity(ContentTypes.`image/png`, httpData).asInstanceOf[HttpEntity.NonEmpty]
val formFile = FormFile("my-image", httpEntity)
val bodyPart = BodyPart(formFile, "my-image")
val req = Post(url, MultipartFormData(Map("spray-file" -> bodyPart)))
val pipeline = (addHeader("Content-Type", "multipart/form-data")
~> sendReceive
)
pipeline(req)
但是如何同时发送文件和字段呢?