How could I upload files in ExpressJS 4.x

前端 未结 3 585
野的像风
野的像风 2020-12-16 02:33

I know there are something like multiparty, and busboy and formidable. I want to know is there any chance to upload files without them.

相关标签:
3条回答
  • 2020-12-16 02:46

    Without them, parsing multipart forms can be hard to do right and efficiently. Unless you really want to go through that work of parsing multipart yourself (urlencoded forms are much easier), you really should stick to using one of connect-multiparty, multer, connect-busboy, reformed, etc.

    0 讨论(0)
  • 2020-12-16 02:47

    Standalone parsers like busboy can be used with or without Express.js. On the other hand Express.js middlewares augment the request object by adding req.files that you can conveniently access the files. Some implementations store intermediate files either in temp directory or in memory. Others let you access stream of uploaded file contents without busting the server's memory or hard disk.

    It's possible to implement parsing multipart/form-data HTTP request yourself and it's a fine programming exercise. However, if development time, maintainability and bug fixes are of concern, then ready-made libraries come in handy.

    Depending on your situation, ask these questions in order to determine which library suits you best.

    • is saving intermediate files ok, or do you want to stream the files?
    • if saving intermediate files is alright, would you prefer them in memory or on hard disk?

    Then pick one from the list of most used file upload processing libraries. The decision tree is from the article:

    Choose between Formidable, Busboy, Multer and Multiparty for processing file uploads

    Choose between Formidable, Busboy, Multer and Multiparty for processing file uploads

    0 讨论(0)
  • 2020-12-16 02:57

    If you want to upload files you need to be able to parse multipart content. You can either use to parse it or implement your own. Here's a list the modules that can help you:

    Direct parsing (in descending popularity based on Github stars as of Nov 2016):

    • formidable
    • busboy
    • multiparty

    Middleware:

    • multer - based on busboy, much more popular than options below
    • busboy middleware - last commit April 2014
    • multiparty middleware - suggests not to use on Github page

    Both:

    • parted - last commit Jan 2015

    If you want to write your own parser, you can have a look how above modules does it. Check these links also:

    • Here's how it looks like
    • multipart protocol spec and html spec
    • Blog post about implementing formidable
    • bop: Boyer Moore Parser, good node module to find the multipart boundaries position
    • Another blog post about implementing multi-part parser

    Happy coding.

    0 讨论(0)
提交回复
热议问题