Giter Site home page Giter Site logo

fastify-file-upload's Introduction

fastify-file-upload

Fastify plugin to upload file.

Build Status NPM version

Note: the v3.x series of this module covers Fastify v3. For Fastify v2 support refert to the v2.x series.

Install

npm i fastify-file-upload --save

Usage

'use strict'

const fastify = require('fastify')()
const fileUpload = require('fastify-file-upload')

fastify.register(fileUpload)

fastify.post('/upload', function (req, reply) {
  // some code to handle file
  const files = req.raw.files
  console.log(files)
  let fileArr = []
  for(let key in files){
    fileArr.push({
      name: files[key].name,
      mimetype: files[key].mimetype
    })
  }
  reply.send(fileArr)
})

fastify.listen(3000, err => {
  if (err) throw err
  console.log(`server listening on ${fastify.server.address().port}`)
})

Use Schema Demo (fastify version >= 2)

fastify.post('/uploadSchema', {
  schema: {
    summary: 'upload file',
    body: {
      type: 'object',
      properties: {
        file: { type: 'object' }
      },
      required: ['file']
    }
  },
  handler: (request, reply) => {
    const file = request.body.file
    console.log(file)
    reply.send({ file })
  }
})

Using Busboy Options

fastify.register(fileUpload, {
  limits: { fileSize: 50 * 1024 * 1024 },
});

fastify-file-upload's People

Contributors

huangang avatar nobdod avatar sadart avatar tobilen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

fastify-file-upload's Issues

Missing data in body (ignored other values to files)

We're sending data as in example bellow, due to this line:

     if (request.raw.files) {
        for (const key in request.raw.files) {
          request.body[key] = request.raw.files[key]
        }
      }

They got ignored and therefore not added into body, so I hope this helps somebody, @huangang maybe consider adding following 🙂

import fp from 'fastify-plugin'
import fileUpload from 'express-fileupload'

const setMultipart = (request, done): void => {
  request[Symbol('multipart')] = true
  done()
}

const fastifyUpload = (fastify, options, done) => {
  fastify.addContentTypeParser('multipart', setMultipart)

  options = options || {}
  fastify.use(fileUpload(options))

  fastify.addHook('preValidation', (request, reply, done) => {
    if (request.raw) {
      !request.body && (request.body = request.raw.body || {})
      if (request.raw.files) {
        request.body = { ...request.raw.body, ...request.raw.files }
      }
    }
    done()
  })
  done()
}

export default fp(fastifyUpload, {
  fastify: '>= 2.0.0',
  name: 'fastify-file-upload'
})

Example request

PUT http://localhost:3000/registration/938cecf7-fa10-4580-a4bd-978ca34f3b67
Content-Type: multipart/form-data; boundary=WebAppBoundary
x-token: vDkhBTJCwsdov0t55bzu4UHIN9AXZgYJGz3Fq326Rske8ZCm

--WebAppBoundary
Content-Disposition: form-data; name="files"; filename="logo.png"
Content-Type: image/png

< ./input.pdf

--WebAppBoundary
Content-Disposition: form-data; name="files"; filename="logo.png"
Content-Type: image/png

< ./input.png

--WebAppBoundary
Content-Disposition: form-data; name="data"

{}
--WebAppBoundary--

How to use schemas with file "fastify-file-upload"?

I've fasted with a question about route's schema with uploading file. What type of JsonSchema should I use for this? For example:
headers: { type: 'object', properties: { 'x-user-id': { type: 'string' }, }, required: ['x-user-id'] }, body: { type: 'object', properties: { ??? } }, description: 'Uploading file', tags: ['Files'], response: { ... }

BUG

"fastify": "^3.27.4",
"fastify-file-upload": "^3.0.1"
进程阻断 无法正常启动

Unable to receive file data

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.

request files - array or array array

Hello, i have a problem
If i send one file, i got Array

[
  {
    name: 'zzzzzzz.jpg',
   ............
  }
]

If i send several files i got Array Array

[
  [
    {
      name: 'zzzzzzz.jpg',
...............................
    },
    {
      name: 'zzzzzzz2.jpg',
.......................
    }
  ]
]

It's problem i think, uncomfortably

TypeScript support

Hi there,

just wonder if there any plans to support typescript or not?

Clean exit

how i use this, i follow the docs but fastify wont connect

Action required: Greenkeeper could not be activated 🚨

🚨 You need to enable Continuous Integration on Greenkeeper branches of this repository. 🚨

To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because it uses your CI build statuses to figure out when to notify you about breaking changes.

Since we didn’t receive a CI status on the greenkeeper/initial branch, it’s possible that you don’t have CI set up yet.
We recommend using:

If you have already set up a CI for this repository, you might need to check how it’s configured. Make sure it is set to run on all new branches. If you don’t want it to run on absolutely every branch, you can whitelist branches starting with greenkeeper/.

Once you have installed and configured CI on this repository correctly, you’ll need to re-trigger Greenkeeper’s initial pull request. To do this, please click the 'fix repo' button on account.greenkeeper.io.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.