본문 바로가기

Flutter

[Flutter] MultipartFile 전송 에러 - (HttpException: Content size below specified contentLength)

MultipartFile.fromPath(field, filePath)

Flutter http 모듈 사용시 이미지 / 파일 등 multifile 파트를 이용해 파일을 전송할 때 가끔 발생하는 에러가 있습니다.

HttpException: Content size below specified contentLength. 473320 bytes written but expected 558138

 

문제 원인은 아래 코드였습니다.

MultipartFile.fromFile(filePath)

 

async / await 문을 이용하여 파일을 정상적으로 필드에 집어넣더라도 비동기 오류가 발생하는듯 합니다.

MultipartFile.fromBytes("images", file.readAsBytesSync(), filename: file.path.split("/").last);

 

위의 함수로 교체하여 해결되었습니다.

For문 대신 ForEach나 List.generate 사용시에도 비슷한 문제가 발생한적 있으니

아마 비동기 오류가 맞을듯합니다. 비슷한 에러 해결하신적 있으면 댓글 부탁드립니다