0
我知道这个问题以前可能已经被问过,但它不适用于我。如何使用retrofit2发送json对象的文件
我的代码:
@Multipart
@PUT("/update")
fun updateClass(
@Part("data") myData: RequestBody,
@Part file: MultipartBody.Part? = null
): Call<String>
val imgFile = File(part)
val mFile = RequestBody.create(MediaType.parse("multipart/form-data"), imgFile)
val fileToUpload = MultipartBody.Part.createFormData("file", imgFile.name, mFile)
val myData = RequestBody.create(MediaType.parse("application/json"), json)
retrofit.create(InterfaceRetrofit::class.java).updateClass(myData, fileToUpload).execute()
,在这里我的NodeJS控制台日志
{ data: '{"id":1,"name":"hhhhhhhhhhh"}' }
{ file:
{ fieldName: 'file',
originalFilename: 'IMG_20170730_223331.jpg',
path: 'C:\\Users\\lucius\\AppData\\Local\\Temp\\fT-jA8iemxi50AZZG7CM0q2J.jpg',
headers:
{ 'content-disposition': 'form-data; name="file"; filename="IMG_20170730_223331.jpg"',
'content-type': 'multipart/form-data',
'content-length': '29388' },
size: 29388,
name: 'IMG_20170730_223331.jpg',
type: 'multipart/form-data' } }
是,如果可能的话我想是这样的:
{ data: {"id":1,"name":"hhhhhhhhhhh"} }....
感谢
我知道我可以在nodejs代码中做到这一点。 我只是想找到解决方案,以防我不能这样做:)谢谢 – lucius165