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

Returning files from load function does not work #409

Open
1 task done
szethh opened this issue Apr 15, 2024 · 2 comments
Open
1 task done

Returning files from load function does not work #409

szethh opened this issue Apr 15, 2024 · 2 comments

Comments

@szethh
Copy link

szethh commented Apr 15, 2024

  • Before posting an issue, read the FAQ and search the previous issues.

Description
I want to load some default data from a database to initially populate the form. This includes some files.

This seems to work server-side. However, the client does not receive the files. This is inside my load function:

// an example of a file that would come from s3 or whatever
const data = {
  files: [
    new File(['hello'], 'hello.txt', { type: 'text/plain' })
  ]
}

// we validate
const form = await superValidate(data, zod(schema))

console.log('valid on server', form.valid)  // true
console.log('files on server', form.data.files)  // [<the file object we created above>]

return withFiles({ form });

On the client:

$: console.log('files in client', $form.files) // []

If I have more fields in my schema/initial data, those pass through just fine. There are no validation errors anywhere, but the files array never gets to the client.

If applicable, a MRE
Use this template project to create a minimal reproducible example that you can link to here: https://sveltelab.dev/github.com/ciscoheat/superforms-examples/tree/zod (right click to open in a new tab)

See the console logs in the repro. The file is validated fine in the server, but the client does not receive it.
https://www.sveltelab.dev/zqpke6y03pcgu3s

@szethh szethh added the bug Something isn't working label Apr 15, 2024
@szethh
Copy link
Author

szethh commented Apr 15, 2024

just saw the implementation of withFiles, turns out it actually just removes the files.

is there another way of sending files as initial values?

my use case is a form that is submitted with some attachments, and then can be edited again.
the attachments would be sent from the server to the client such that the attachments can be edited.

i can do this with an api route that i fetch from the client but i was hoping there was an "intended" (superforms-wise) way to do it.

@ciscoheat
Copy link
Owner

SvelteKit cannot serialize File objects, so you need to handle it yourself, base64-encode the data for example and send it as a string in the load function data, then create the File object on the client before calling superForm.

@ciscoheat ciscoheat removed the bug Something isn't working label Apr 19, 2024
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