Dio: Content size exceeds specified contentLength on formdata upload

Created on 23 May 2019  Â·  30Comments  Â·  Source: flutterchina/dio

=====================================

var formData = new FormData.from({
          "param1": "-1",
          "param2": "-1",
          "param3": "-1",
          "param4": "-1",
          "param5": "-1",
          "music": new UploadFileInfo(new File(fileUrl), "audio.m4a"),
        });

 final response = await _api.post(uploadUrl, data:formData,
        onSendProgress: (sent, total) {
      print("uploadFile ${sent / total}");
    });

  • fileUrl comes from device storage.
  • fileUrl is valid, as upload process begins, and it was tested;

Produces this error:

flutter: DioError [DioErrorType.DEFAULT]: HttpException: Content size exceeds specified contentLength. 234777 bytes written while expected 234769. [----dio-boundary-0252882641--]
Can someone help we with this?

Most helpful comment

this error appears on version 2.1.8 also @wendux

Unhandled Exception: DioError [DioErrorType.DEFAULT]: HttpException: Content size exceeds specified contentLength. 282 bytes written while expected 194.

All 30 comments

Dio version?

I believe this error appears with the dio version 2.1.4

dio: ^2.1.5

flutter doctor -v:

[✓] Flutter (Channel stable, v1.5.4-hotfix.2, on Mac OS X 10.14.4 18E226, locale
en-US)
• Flutter version 1.5.4-hotfix.2 at /Users/mac/flutter/flutter
• Framework revision 7a4c33425d (4 weeks ago), 2019-04-29 11:05:24 -0700
• Engine revision 52c7a1e849
• Dart version 2.3.0 (build 2.3.0-dev.0.5 a1668566e5)

[✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
• Android SDK at /Users/mac/Library/Android/sdk
• Android NDK location not configured (optional; useful for native profiling
support)
• Platform android-28, build-tools 28.0.3
• ANDROID_SDK_ROOT = /Users/mac/Library/Android/sdk
• Java binary at: /Applications/Android
Studio.app/Contents/jre/jdk/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build
1.8.0_152-release-1248-b01)
• All Android licenses accepted.

[✓] iOS toolchain - develop for iOS devices (Xcode 10.2)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 10.2, Build version 10E125
• ios-deploy 1.9.4
• CocoaPods version 1.5.3

[✓] Android Studio (version 3.3)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin version 32.0.1
• Dart plugin version 182.5215
• Java version OpenJDK Runtime Environment (build
1.8.0_152-release-1248-b01)

[✓] VS Code (version 1.31.1)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 2.25.1

[✓] Connected device (1 available)
• iPhone 5s • 0D607B15-AC72-4268-92D2-3A142D54D9DC • ios •
com.apple.CoreSimulator.SimRuntime.iOS-12-2 (simulator)

@wendux any ideas? if you need more info, let me know how I can help..

@vinters768 The value of Content-length is formData.length. you can test with follow code:

 var t= await formData.asBytesAsync();  // Stream size.
 print(formData.length==t.length);  // Should be `true`

If formData.length is not equal tot.length, please upload the "audio.m4a" and I'll help you to check it.

@wendux here is what i got:

 var fileLenght = file.lengthSync();

        print("fileLenght = $fileLenght");

        var formStreamSize = await formData.asBytesAsync(); // Stream size.

        print("formStreamSize = ${formStreamSize.length}");
        print("formData.length = ${formData.length}");

        print("formData.length == formStreamSize.length ${formData.length == formStreamSize.length}"); // Should be `true`
flutter: fileLenght = 239166
flutter: formStreamSize = 240231
flutter: formData.length = 240223



md5-b9fc998529aaaa85b8974a89b7391833



flutter: DioError [DioErrorType.DEFAULT]: HttpException: Content size exceeds specified contentLength. 240231 bytes written while expected 240223. [----dio-boundary-1735630428--]

"audio.m4a" file url: https://github.com/flutterchina/dio/files/3222464/final.m4a.zip

thanks for your help

@wendux sorry for the trouble, but we are kind waiting on this for production release.. could you please confirm that this isn't a problem on dio side?

thanks again

@vinters768 This is my test code:

import 'dart:io';
import 'package:dio/dio.dart';

main() async {
  var formData = new FormData.from({
    "param1": "-1",
    "param2": "-1",
    "param3": "-1",
    "param4": "-1",
    "param5": "-1",
    "music": new UploadFileInfo(new File("./example/audio.m4a"), "audio.m4a"),
  });
  var t= await formData.asBytesAsync();
  print("formStreamSize = ${t.length}"); 
  print("formData.length = ${formData.length}"); //
  print(formData.length==t.length);

}

The output is:

formStreamSize = 239748
formData.length = 239748
true

Any other params in formdata ?
BTY, could you downgrade to v2.1.3 and have a try.

@wendux the only difference is that "file" comes from device storage, not flutter project path.. will try v2.1.3 and let you now.

@wendux downgrade worked.. don't know why though.

anyway, thanks again!

this error appears on version 2.1.7 also @wendux

same proplem here 2.1.7

Same problem here 2.1.7. The downgrade to 2.1.3 resolved my problem.

fixed in v2.1.8

this error appears on version 2.1.8 also @wendux

Unhandled Exception: DioError [DioErrorType.DEFAULT]: HttpException: Content size exceeds specified contentLength. 282 bytes written while expected 194.

This exception happens to me, either. It only happens when I test my app on Android but not IOS. Wish these details can help.

Same thing happen to me with an audio file.
My dio version is 2.1.13

Same thing happen to me with an audio file.
My dio version is 2.1.13

same, but with .txt file

same error for dio: 2.1.16

please update to 3.0.x and try again .

:octocat: From gitme Android

please update to 3.0.x and try again .

:octocat: From gitme Android

Hi @wendux, this is happening in 3.0.3 version too.

Content size exceeds specified contentLength. 66130 bytes written while expected 627
error caught while uploading PDF

dio: ^3.0.8

Same problem in 3.0.9

Content size exceeds specified contentLength. 418 bytes written while expected 257. in text files

dio 3.0.9

I have the same problem in version 3.0.9 I can't solve

Hi guys, anyone got the answer for this issue yet? still happening on version 3.0.9 with audio file.

@petra-bios It had been a year that this issue isn't solved still its status is closed
and most of us tried but din't get what we want so don't stuck on same problem try alternative for audio files request

Hi @AdnanKazi , I think i found the solution for this (at least for the case i'm facing).
I will leave it here in case other people need it.

what solved mine was:
I use MultipartFile.fromBytes instead of MultipartFile.fromFile

For example :
from this :
final file = await MultipartFile.fromFile(filePath);
to this :
final file = MultipartFile.fromBytes(File(filePath).readAsBytesSync(), filename: filePath.split("/").last);

Thanks and i hope it helps.

@petra-bios Thank you very much for solution i will try it in mean time

@petra-bios awesome save my day!
Follow my solution with mp3:

FormData formData = FormData.fromMap({
      "file": MultipartFile.fromBytes(
          File(recordFilePath).readAsBytesSync(),
          filename: fileName),
    });
Was this page helpful?
0 / 5 - 0 ratings