Last active
March 28, 2021 14:55
-
-
Save paambaati/db2df71d80f20c10857d to your computer and use it in GitHub Desktop.
Uploading files using NodeJS and Express 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<body> | |
<form action="/upload" enctype="multipart/form-data" method="post"> | |
<input type="text" name="title"> | |
<input type="file" name="file"> | |
<input type="submit" value="Upload"> | |
</form> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* NodeJS code. | |
*/ | |
// Required modules. | |
var express = require('express'), | |
http = require('http'), | |
formidable = require('formidable'), | |
fs = require('fs'), | |
path = require('path'); | |
var app = express(); | |
// All your express server code goes here. | |
// ... | |
// Upload route. | |
app.post('/upload', function(req, res) { | |
var form = new formidable.IncomingForm(); | |
form.parse(req, function(err, fields, files) { | |
// `file` is the name of the <input> field of type `file` | |
var old_path = files.file.path, | |
file_size = files.file.size, | |
file_ext = files.file.name.split('.').pop(), | |
index = old_path.lastIndexOf('/') + 1, | |
file_name = old_path.substr(index), | |
new_path = path.join(process.env.PWD, '/uploads/', file_name + '.' + file_ext); | |
fs.readFile(old_path, function(err, data) { | |
fs.writeFile(new_path, data, function(err) { | |
fs.unlink(old_path, function(err) { | |
if (err) { | |
res.status(500); | |
res.json({'success': false}); | |
} else { | |
res.status(200); | |
res.json({'success': true}); | |
} | |
}); | |
}); | |
}); | |
}); | |
}); |
Awesome. Really helped me. Thanks
This was so helpful! Thank you!
it doesn't work....after i click the upload button, it's direct me to another blank page....can you help me with the problem????
i'm beginner with NodeJS......
Where I will get the filepath on the node server ??
DD
this is too lengthy can u please shorten the code
i have followed the method but i am not able to see my file on the url which i have sent.
curl -s -v --form [email protected] https://URL/upload
I llike it!
Tried to clear some things up to make it more understandable:
import path from 'path'
import express from 'express'
import formidable from 'formidable'
let app = express()
app.post('/upload', (req, res) => {
let form = new formidable.IncomingForm()
/**
* Options
*/
form = Object.assign(form, {
multiples: true,
keepExtensions: true,
uploadDir: path.join(__dirname, '../uploads/'), // Set standard upload dir
encoding: 'utf-8',
type: 'multipart', // or urlencoded
maxFieldsSize: 20 * 1024 * 1024, // default = 20 * 1024 * 1024 = 20mb
maxFields: 1000, // Max files & fields - default = 1000
hash: false, // sha1, md5 or false
// @note - Disable field & file event listeners and let you handle upload yourself
onPart (part) {
part.addListener('data', packet => {
// console.log('Packet received', packet.toString()) // Raw packet data
// packet_a + packet_b + packet_c + ... = file data
})
// Handle part / file only if .mov is not included in filename
if (part.filename && part.filename.indexOf('.mov') === -1) {
form.handlePart(part)
// Or if filename is not set
} else if (!part.filename) {
form.handlePart(part)
}
}
})
/**
* Events
*/
form.on('fileBegin', (name, file) => {
// file.name - basename with extension
// file.size - currently uploaded bytes
// file.path - beeing written to
// file.type - mime
// file.lastModifiedDate - date object or null
// file.hash - hex digest if set
// Changing file upload path can also be done here:
file.path = path.join(__dirname, '../uploads_secondary/' + file.name)
})
form.on('progress', (bytesReceived, bytesExpected) => {
console.log('Progress', bytesReceived, bytesExpected)
})
form.on('error', (err) => {
console.error(err)
})
form.on('aborted', () => {
console.error(new Error('Aborted'))
})
form.on('end', () => {
console.log('End')
})
form.on('field', (name, value) => {
console.log('Field', name, value)
})
form.on('file', (name, file) => {
console.log('File', name, file.type)
})
/**
* Function
*
* Passes request from express to formidable for handling.
* Second arg is a callback executed on complete & returns all data
*
*/
form.parse(req, (err, fields, files) => {
if (err) {
return res.status(500).json({ error: err })
} else {
return res.status(200).json({ uploaded: true })
}
})
})
app.listen(4000)
@cannap super useful
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi i know this is a bit older but now its much simpler 💃