Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix file upload through htmx.ajax #2778

Merged
merged 2 commits into from
Aug 5, 2024
Merged

Conversation

Telroshan
Copy link
Collaborator

@Telroshan Telroshan commented Jul 31, 2024

Fixes #2630

Description

When converting an object to FormData, we had a simple check that if a value was an object, we'd JSON-stringify it before appending it to the FormData.

#2748 addressed a similar issue, thoughI had totally missed that a similar check was done elsewhere in the code.

One case was not handled here, causing the referenced issue ; the Blob type, that FormData can perfectly handle (a type that includes Files), would be JSON-stringified instead of directly passed to FormData.append as it's an object itself, resulting in the loss of the file data, and that conversion to an empty object.

Corresponding issue: #2630

Testing

Added a test cases, to ensure that htmx.ajax with a file passed in parameters, is correctly uploaded and not empty.
Also added another test case with a file input as I found out we didn't have any yet.

Additionnaly, you can:

  • Observe the current behavior on this JSFiddle, notice how the file is an empty object instead of a binary
  • Observe the behavior with this PR's fix on this JSFiddle, notice how the file is now correctly a binary

Checklist

  • I have read the contribution guidelines
  • I have targeted this PR against the correct branch (master for website changes, dev for
    source changes)
  • This is either a bugfix, a documentation update, or a new feature that has been explicitly
    approved via an issue
  • I ran the test suite locally (npm run test) and verified that it succeeded

@Telroshan Telroshan added bug Something isn't working 2.0 ready for review Issues that are ready to be considered for merging labels Jul 31, 2024
@Telroshan Telroshan changed the title Fix ajax file Fix file upload through htmx.ajax Jul 31, 2024
@1cg 1cg merged commit 941e94f into bigskysoftware:dev Aug 5, 2024
1 check passed
@Telroshan Telroshan deleted the fix-ajax-file branch August 5, 2024 21:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.0 bug Something isn't working ready for review Issues that are ready to be considered for merging
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants