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

Feature: support file uploads #53

Closed
1 of 3 tasks
daun opened this issue Aug 17, 2023 · 5 comments
Closed
1 of 3 tasks

Feature: support file uploads #53

daun opened this issue Aug 17, 2023 · 5 comments

Comments

@daun
Copy link
Member

daun commented Aug 17, 2023

Describe the problem

We should go ahead and implement file uploads — it's simple enough with fetch now.

Describe the proposed solution

Append attached files to the request body:

const input = document.querySelector('input[type="file"]')
const data = new FormData()
for (const file of input.files) {
  data.append(input.name, file)
}

fetch('/', { method: 'POST', body: data })

Alternatives considered

Keep recommending to use the API directly for file uploads.

How important is this feature to you?

  • Nice to have
  • Would make my life a lot easier
  • I cannot use swup without it
@daun
Copy link
Member Author

daun commented Aug 18, 2023

Looks like this is already supported :) Creating a FormData obj from a form element apparently includes file inputs.

@daun daun closed this as completed Aug 18, 2023
@hirasso
Copy link
Member

hirasso commented Aug 18, 2023

I remember a bug in Safari where empty file inputs in AJAX-submitted forms would cause the request to fail. Back then I used this hack to fix the issue.

We should test sometime if this problem was solved in recent versions of Safari.

Should I create an issue for this?

@daun
Copy link
Member Author

daun commented Aug 18, 2023

Let‘s maybe just reopen this issue to keep track of it? And test in Safari. Could this have to do with XMLHttpRequest instrad of fetch? In any case, let‘s make sure!

@daun daun reopened this Aug 18, 2023
@hirasso
Copy link
Member

hirasso commented Aug 18, 2023

It could, yes! Need a project to test it 😅

@daun
Copy link
Member Author

daun commented Aug 18, 2023

I think we're safe — looks like the issue was solved in a Safari release in 2018, i.e. it only affects Safari 11 which we currently don't support.

@daun daun closed this as completed Aug 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants