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

Unable to receive file data #8

Open
Turbine1991 opened this issue Aug 25, 2020 · 1 comment
Open

Unable to receive file data #8

Turbine1991 opened this issue Aug 25, 2020 · 1 comment

Comments

@Turbine1991
Copy link

Turbine1991 commented Aug 25, 2020

I'm unable to loop through the files at the fastify end.

I've created a simple form with the help of bulma:

<form id="form-upload" action="/upload/submit" method="post">
  <div id="file-js-example" class="file has-name">
    <label class="file-label">
      <input class="file-input" type="file" name="files" id="files">
      <span class="file-cta">
        <span class="file-icon">
          <i class="fas fa-upload"></i>
        </span>
        <span class="file-label">
          Choose a file…
        </span>
      </span>
      <span class="file-name">
        No file uploaded
      </span>
    </label>
  </div>
</form>

Here is the fastify code:

www.fastify.post('/upload/submit', function (req, reply) {
  console.log(req.raw.files); // This returns null
});

I presume that I've done something wrong in creating the HTML form?
Thank you.

@flo-sch
Copy link

flo-sch commented Jan 18, 2022

If you use the HTML form action to call your backend endpoint, then you probably need to add enctype="multipart/form-data" attribute on it, so the browser knows the request should be sent as multipart and the backend can parse files.

Ref: https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/enctype

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