For more detail, please visit:
Node.js Express File Upload with Google Cloud Storage example
Front-end Apps to work with this Node.js Server:
-
Angular 8 / Angular 10 / Angular 11 / Angular 12 / Angular 13 / Angular 14 / Angular 15 / Angular 16 Client
-
Angular Material 12 / Angular Material 14 / Angular Material 15 / Angular Material 16
More Practice:
Integration (run back-end & front-end on same server/port)
npm install
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
})
})
})
})
}
`