Skip to content

Node.js Express Upload/Download File Rest APIs example with Multer

Notifications You must be signed in to change notification settings

John-Tonny/express-file-upload

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Node.js Express Upload/Download file Rest API

For more detail, please visit:

Node.js Express File Upload Rest API example

Node.js Express Download File Rest API example

Node.js Express Delete File Rest API example

Node.js Express File Upload with Google Cloud Storage example

Node Express File Upload to MongoDB example

Front-end Apps to work with this Node.js Server:

More Practice:

Node.js: Upload/Import Excel file data into Database

Node.js: Upload/Import CSV file data into Database

Node.js Rest APIs example with Express & MySQL

Node.js Rest APIs example with Express & PostgreSQL

Node.js Rest APIs example with Express & MongoDB

Node.js JWT Authentication & Authorization example

Deploying/Hosting Node.js app on Heroku with MySQL database

Integration (run back-end & front-end on same server/port)

Integrate React with Node.js Restful Services

Integrate Angular with Node.js Restful Services

Integrate Vue with Node.js Restful Services

Project setup

npm install

Run

node server.js


### update
node 16.18.0
修改node_modules/multer/storage/disk.js

DiskStorage.prototype._handleFile = function _handleFile (req, file, cb) {
  var that = this

  that.getDestination(req, file, function (err, destination) {
    if (err) return cb(err)

    that.getFilename(req, file, function (err, filename) {
      if (err) return cb(err)

      var fsHash = crypto.createHash('sha256')
      var finalPath = path.join(destination, filename)
      var outStream = fs.createWriteStream(finalPath)
      file.stream.pipe(outStream)
      outStream.on('error', cb)
      file.stream.on('data', function (data) {
        fsHash.update(data)
      })
      outStream.on('finish', function () {
        var newfilename = fsHash.digest('hex') + path.extname(filename)
        if(fs.existsSync(path.join(destination, newfilename))){
          fs.unlinkSync(finalPath)
        }else{
          fs.renameSync(finalPath, path.join(destination, newfilename))
        }
        return cb(null, {
          destination: destination,
          filename: newfilename,
          path: finalPath,
          size: outStream.bytesWritten
        })
      })
    })
  })
}

`

About

Node.js Express Upload/Download File Rest APIs example with Multer

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%