Created
March 4, 2021 14:23
-
-
Save stephanbogner/ec0d906aac81e0782703f183821d0615 to your computer and use it in GitHub Desktop.
Simple example of sending files (images in my case) with jQuery (or HTML form) to a NodeJS Express backend using Multer
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
// Code for client | |
// Should be put inside a folder called 'public', so Express serves it (see line 12 in index.js) | |
<input id="hiddenFileInput" style="display:none;" type="file" multiple> | |
<button id="button">Upload files</button> | |
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script> | |
<script type="text/javascript"> | |
$('#button').on('pointerdown', () => { | |
$('#hiddenFileInput').click(); //Used so we can use a custom button for file upload | |
}) | |
$('input[type=file]').change(function () { | |
var files = this.files; | |
var fieldNameImportantForMulter = 'media'; | |
var formData = new FormData(); | |
for (var i = 0; i < files.length; i++) { | |
var file = files[i]; | |
formData.append(fieldNameImportantForMulter, file, file.name); | |
} | |
// Via https://stackoverflow.com/a/5976031/3763660 | |
$.ajax({ | |
cache: false, | |
contentType: false, | |
processData: false, | |
method: 'POST', | |
url : '/api', | |
data : formData, | |
success : function(response){ | |
console.log('Success', response); | |
}, | |
error : function(error){ | |
console.log('Error', error); | |
} | |
}) | |
}); | |
</script> |
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 – you have to npm install Express and Multer before it works | |
const express = require('express'); | |
// BodyParser does not parse "multipart/form-data" which multiple files are (https://stackoverflow.com/a/23599340/3763660) | |
var multer = require('multer'); // Multer is a wrapper for express built on top of busboy for "multipart/form-data" | |
var upload = multer(); | |
const app = express() | |
const port = 3000 | |
const fieldNameSpecifiedInForm = 'media'; | |
const maxNumberOfFiles = 40; | |
app.use('/', express.static('public')); //from https://expressjs.com/en/starter/static-files.html | |
app.post('/api', upload.array(fieldNameSpecifiedInForm, maxNumberOfFiles), function (req, res, next) { | |
// req.files is array of `media` files | |
// req.body will contain the text fields, if there were any | |
console.log(req.files, req.body); | |
let files = req.files; | |
for(let index in files){ | |
let file = files[index]; | |
console.log(file.originalname) | |
} | |
res.send("Data received"); | |
}) | |
app.listen(port, () => { | |
console.log(`Example app listening at http://localhost:${port}`) | |
}) |
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
// Code for client (alternative) | |
// Does the same thing as index.html but with HTML only | |
<form action="/api" enctype="multipart/form-data" method="post"> | |
<input type="file" name="media" multiple> | |
<input type="submit" value="Send data"> | |
</form> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment