I have created an application using an android studio and for server-side using NodeJs.I have tried to pass a single image and able to pass. This time, I want to pass multiple images to the server, but when I started change parameter from image:MultipleBody.Part
to images:List<MultipartBody.Part>
at Api.kt file, received an error message at the server-side. I don't know with a part that I'm doing it wrong. Hope can help me solve this problem. Thank you in advance.
$ node app Listening on port 4545... MulterError: Unexpected field at wrappedFileFilter (C:\Users\user\Documents\GitHub\EIS-API\node_modules\multer\index.js:40:19) at Busboy. (C:\Users\user\Documents\GitHub\EIS-API\node_modules\multer\lib\make-middleware.js:114:7) at Busboy.emit (events.js:223:5) at Busboy.emit (C:\Users\user\Documents\GitHub\EIS-API\node_modules\busboy\lib\main.js:38:33) at PartStream. (C:\Users\user\Documents\GitHub\EIS-API\node_modules\busboy\lib\types\multipart.js:213:13) at PartStream.emit (events.js:223:5) at HeaderParser. (C:\Users\user\Documents\GitHub\EIS-API\node_modules\dicer\lib\Dicer.js:51:16) at HeaderParser.emit (events.js:223:5) at HeaderParser._finish (C:\Users\user\Documents\GitHub\EIS-API\node_modules\dicer\lib\HeaderParser.js:68:8) at SBMH. (C:\Users\user\Documents\GitHub\EIS-API\node_modules\dicer\lib\HeaderParser.js:40:12) MulterError: Unexpected field at wrappedFileFilter (C:\Users\user\Documents\GitHub\EIS-API\node_modules\multer\index.js:40:19) at Busboy. (C:\Users\user\Documents\GitHub\EIS-API\node_modules\multer\lib\make-middleware.js:114:7) at Busboy.emit (events.js:223:5) at Busboy.emit (C:\Users\user\Documents\GitHub\EIS-API\node_modules\busboy\lib\main.js:38:33) at PartStream. (C:\Users\user\Documents\GitHub\EIS-API\node_modules\busboy\lib\types\multipart.js:213:13) at PartStream.emit (events.js:223:5) at HeaderParser. (C:\Users\user\Documents\GitHub\EIS-API\node_modules\dicer\lib\Dicer.js:51:16) at HeaderParser.emit (events.js:223:5) at HeaderParser._finish (C:\Users\user\Documents\GitHub\EIS-API\node_modules\dicer\lib\HeaderParser.js:68:8) at SBMH. (C:\Users\user\Documents\GitHub\EIS-API\node_modules\dicer\lib\HeaderParser.js:40:12)
Api.kt
@Multipart
@POST("perkhidmatan_rumput/api/PostPemantauanPerkhidmatanPotingRumput" +
"/{zon}/{syarikat}/{alamat_syarikat}/{nama_penyelia}/{taman}/{bulan}/{tahun}" +
"/{masa}/{timeAMPM}/{pusingan}/{status}/{catatan}/{state}/{entryOperator}")
fun postPemantauanPerkhidmatanPotingRumput(
@Path("zon")zon:String,
@Path("syarikat")syarikat:String,
@Path("alamat_syarikat")alamat_syarikat:String,
@Path("nama_penyelia")nama_penyelia:String,
@Path("taman")taman:Int,
@Path("bulan")bulan:String,
@Path("tahun")tahun:String,
@Path("masa")masa:String,
@Path("timeAMPM")timeAMPM:String,
@Path("pusingan")pusingan:String,
@Path("status")status:String,
@Path("catatan")catatan:String,
@Path("state")state:String,
@Path("entryOperator")entryOperator:String,
@Part images:List<MultipartBody.Part>
//@Part image:MultipartBody.Part?
):LiveData<GenericApiResponse<OnResponse>>
PemoViewModel.kt
public fun getAllImages():List<MultipartBody.Part>?{
var info = getCurrentViewStateOrNew()
var pic:List<GambarSebelum>? = info.gambarSebelum
var images = mutableListOf<MultipartBody.Part>()
pic?.let {
for ( item in it){
var multipartBody: MultipartBody.Part? = null
item.url?.path?.let { filePath ->
val imageFile = File(filePath)
val requestBody =
RequestBody.create(
MediaType.parse("image/*"),
imageFile
)
multipartBody = MultipartBody.Part.createFormData(
"image",
imageFile.name,
requestBody
)
}
images.add(multipartBody!!);
}
}
return images;
}
PemoFragment.kt
_binding.btnLogin.setOnClickListener {
if(!isFill()){
Toast.makeText(requireContext(),"sila penuhkan ruang diatas..",Toast.LENGTH_SHORT).show()
return@setOnClickListener
}
var bulan:String = CALENDER.valueOf(_binding.idBulanSpinner.selectedItem.toString()).value
var taman:Int? = _viewModel.getTamanIdByTamanName(_binding.idTamanSpinner.selectedItem.toString())
var pusingan:String? = _viewModel.getPusinganByPusinganName(_binding.idPusinganSpinner.selectedItem.toString())
var status:String? = _viewModel.getStatusByStatusName(_binding.idStatusSpinner.selectedItem.toString())
var images:List<MultipartBody.Part>? = _viewModel.getAllImages()
if(taman != null && pusingan != null && status != null){
//insert information to server (database)
_viewModel.setStateEvent(
FormRumputStateEvent.SubmitPemantauanForm(
PemantauanPotongRumputResponse(
_binding.idZonsSpinner.selectedItem.toString(),
_binding.idNamaSyarikatEdit.text.toString(),
_binding.idAlamatSyarikat.text.toString(),
_binding.idNamaPenyeliaKontraktorEdit.text.toString(),
taman,
bulan,
_binding.idTahunEdit.text.toString(),
_binding.idMasaEdit.text.toString(),
_binding.idAmPmSpinner.selectedItem.toString(),
pusingan,
status,
_binding.idCatatanEdit.text.toString(),
STATE_STATUS.CREATE.name,
"ADMIN",
images!!
)
))
}else{
Toast.makeText(requireContext(),"[Error] Taman,Bulan,pusingan or status has Problem..",Toast.LENGTH_SHORT).show()
return@setOnClickListener
}
}
PemantauanPotongRumputResponse
data class PemantauanPotongRumputResponse(
@Expose
@SerializedName("zon")
val zon:String,
@Expose
@SerializedName("syarikat")
val syarikat:String,
@Expose
@SerializedName("alamat_syarikat")
val alamat_syarikat:String,
@Expose
@SerializedName("nama_penyelia")
val nama_penyelia:String,
@Expose
@SerializedName("taman")
val taman:Int,
@Expose
@SerializedName("bulan")
val bulan:String,
@Expose
@SerializedName("tahun")
val tahun:String,
@Expose
@SerializedName("masa")
val masa:String,
@Expose
@SerializedName("timeAMPM")
val timeAMPM:String,
@Expose
@SerializedName("pusingan")
val pusingan:String,
@Expose
@SerializedName("status")
val status:String,
@Expose
@SerializedName("catatan")
val catatan:String,
@Expose
@SerializedName("state")
val state:String,
@Expose
@SerializedName("entryOperator")
val entryOperator:String,
@Expose
@SerializedName("images")
val images:List<MultipartBody.Part>
)
SERVER SIDE NODE.JS
router.post('/api/PostPemantauanPerkhidmatanPotingRumput/:zon/:syarikat/:alamat_syarikat/'+
':nama_penyelia/:taman/:bulan/:tahun/:masa/:timeAMPM/:pusingan/:status/:catatan/:state/'+
':entryOperator', upload.array('images',9),(req,res,next) =>{
try{
const file = req.files;
if (!file) {
res.status(400).json({
"status": "failed",
"code" : "400",
"message" : "Please upload file"
});
}
res.status(200).json({
"status": "success",
"code" : "200",
"message" : "file uploaded successfully"
});
console.log(file);
console.log(req.body);
}catch(err){
console.log(error.message);
res.status(200).json({
"status": "failed",
"code" : "500",
"message" : error.message
});
}
})