Skip to content

File upload results in: Invalid array length #3633

Closed

Description

@vanlooverenkoen

Describe the bug

For some reason are my file (apk in my case) upload resulting in an Invalid array length

https://github.com/serverpod/serverpod/blob/main/packages/serverpod_client/lib/src/file_uploader.dart#L48

      try {
        var result = await http.post(
          _uploadDescription.url,
          body: await _readStreamData(stream),
          headers: {
            'Content-Type': 'application/octet-stream',
            'Accept': '*/*',
          },
        );
        return result.statusCode == 200;
      } catch (e) {
        return false; // I end up here with an `Invalid array length`  error
      }

To reproduce

This is what i

var uploader = FileUploader(uploadDescription);
final result = await uploader.uploadByteData(ByteData.view(fileBytes.buffer));

Or this

var uploader = FileUploader(uploadDescription);
final fileStream = Stream.value(fileBytes.toList());
final length = fileBytes.length;
final result = await uploader.upload(fileStream, length);

Both of them result false.

Expected behavior

I expect the upload to succeed

Library version

2.8.0

Platform information

Web

Additional context

No response

How experienced are you with this library?

Expert - Experienced and comfortable with using this library in complex projects

Are you interested in working on a PR for this?

  • I want to work on this

Activity

  1. added
    bug
    on Jun 12, 2025
  2. changed the title [-]Upload apk results in: Invalid array length[/-] [+]File upload results in: Invalid array length[/+] on Jun 12, 2025
  3. SandPod commented on Jun 12, 2025

    @SandPod
    Contributor

    Thanks for the report!

    We will try to get to this as soon as possible 👍

  4. added
    area: storage
    on Jun 12, 2025
  5. vanlooverenkoen commented on Jun 17, 2025

    @vanlooverenkoen
    ContributorAuthor

    @SandPod any idea why this is failing? I think my setup is correct (because it works for ipa files, they are smaller so maybe that is the issue?)

    I also have set
    maxRequestSize: 200000000

  6. SandPod commented on Jun 19, 2025

    @SandPod
    Contributor

    Sorry, I have not been able to dig into this one yet.

    I have added it to our list over prioritized bugs. I hope that we can give you a response on this soon.

  7. nielsenko commented on Jun 19, 2025

    @nielsenko
    Collaborator

    @vanlooverenkoen How big is the apk you are trying to upload? What is the uploadDescription passed? (or just the url). I see we don't trace anything about the error, but did you try to set a breakpoint in the catch clause to see the actual exception? I would like to know the exact type, and any related data it might carry.

  8. nielsenko commented on Jun 19, 2025

    @nielsenko
    Collaborator

    What runtime are you using?

  9. vanlooverenkoen commented on Jun 19, 2025

    @vanlooverenkoen
    ContributorAuthor

    The uploadDescription is done exactly how it was documented:
    https://docs.serverpod.dev/concepts/file-uploads#client-side-code

    The apk is 71mb

    The error that I found was Invalid array length, I'm not sure where I can get extra info on this error

  10. vanlooverenkoen commented on Jun 19, 2025

    @vanlooverenkoen
    ContributorAuthor

    What do you mean with runtime?

    develop
    monolith

  11. vanlooverenkoen commented on Jun 19, 2025

    @vanlooverenkoen
    ContributorAuthor

    Let me try and get the runtimeType of that specific error.

  12. vanlooverenkoen commented on Jun 19, 2025

    @vanlooverenkoen
    ContributorAuthor
  13. vanlooverenkoen commented on Jun 19, 2025

    @vanlooverenkoen
    ContributorAuthor

    JSRangeError is the actual error.

  14. 7 remaining items

  15. nielsenko commented on Jun 19, 2025

    @nielsenko
    Collaborator

    Could you check your pubspec.lock? This should give about a factor of 8 in memory, due to the internal Uint8List buffer, which should bring 71MB below the memory use of the 13MB apk you successfully uploaded before.

  16. vanlooverenkoen commented on Jun 19, 2025

    @vanlooverenkoen
    ContributorAuthor

    This is inside my lock file:

      serverpod_client:
        dependency: transitive
        description:
          name: serverpod_client
          sha256: "5865a1430f3eba167fa37fbd543e6975f26592da32ad8d7c5e23e4c15dbf8c4c"
          url: "https://pub.dev"
        source: hosted
        version: "2.8.0"
    

    I also debugged and saw the new code was being used as well

  17. nielsenko commented on Jun 20, 2025

    @nielsenko
    Collaborator

    @vanlooverenkoen This is not what I would expect. It should look like:

      serverpod_client:
        dependency: "direct main"
        description:
          path: "packages/serverpod_client"
          ref: fix-3633-stream-reading
          resolved-ref: "66e401edcd272bef5b30f706d3be60bcce04d023"
          url: "https://github.com/nielsenko/serverpod.git"
        source: git
        version: "2.8.0"

    Did you take a direct dependency on "my" serverpod_client? Did you run dart pub upgrade?

  18. vanlooverenkoen commented on Jun 20, 2025

    @vanlooverenkoen
    ContributorAuthor

    Oh sorry the snippet was from the server pubspec.lock

    This is the frontend
    Image

    This is my pubspec

    dependency_overrides:
      serverpod_client:
        git:
          url: https://github.com/nielsenko/serverpod.git
          ref: fix-3633-stream-reading
          path: packages/serverpod_client
    
  19. vanlooverenkoen commented on Jun 20, 2025

    @vanlooverenkoen
    ContributorAuthor

    I removed the pubspec.lock and I just get the same result.
    When running the app I still get the

    Invalid argument: Invalid array length
    
    JSRangeError
    
  20. vanlooverenkoen commented on Jun 20, 2025

    @vanlooverenkoen
    ContributorAuthor

    @nielsenko wouldn't it make more sense to use a StreamedRequest instead? And upload the requests in chunks?

  21. vanlooverenkoen commented on Jun 20, 2025

    @vanlooverenkoen
    ContributorAuthor

    I just fixed the issue here locally. Let me create a new Pull Request

  22. vanlooverenkoen commented on Jun 20, 2025

    @vanlooverenkoen
    ContributorAuthor
  23. vanlooverenkoen commented on Jun 20, 2025

    @vanlooverenkoen
    ContributorAuthor

    I did not see you already created this one: #3695

    The StreamedRequest does work indeed (at least the PR I created is what worked perfectly)

  24. moved this from Bugs 🐞 to Done 🚀 in Serverpod Frameworkson Jun 20, 2025
  25. nielsenko commented on Jun 20, 2025

    @nielsenko
    Collaborator

    You can try my PR with:

      serverpod_client:
        git:
          url: https://github.com/nielsenko/serverpod.git
          ref: uploader-streaming
          path: packages/serverpod_client

    It should land soon.

  26. nielsenko commented on Jun 20, 2025

    @nielsenko
    Collaborator

    @vanlooverenkoen I wasn't aware you intended to work on this.

  27. vanlooverenkoen commented on Jun 20, 2025

    @vanlooverenkoen
    ContributorAuthor

    No worries, I was just playing around in debugging and accidentally found the fix :D

  28. vanlooverenkoen commented on Jun 20, 2025

    @vanlooverenkoen
    ContributorAuthor

    Yes this works! Also the UI thread is not frozen anymore! Good job @nielsenko

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: storagebug

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions