Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ app.post('/stats', upload.single('uploaded_file'), function (req, res) {



Complete client-side and server-side examples (browser `FormData` uploads of a
blob and of an image) are in the [`examples/`](examples/) folder.

## API

### File information
Expand Down
2 changes: 2 additions & 0 deletions examples/FormDataBlob/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
public/data/uploads/
3 changes: 3 additions & 0 deletions examples/FormDataBlob/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## FormDataBlob

uses the `Blob` constructor to demonstrate sending a blob to the multer middleware on the server.
15 changes: 15 additions & 0 deletions examples/FormDataBlob/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const express = require('express')
const app = express()
const multer = require('multer')

const PORT = 3000

const upload = multer({ dest: './public/data/uploads/' })

app.use('/', express.static('public'))

app.post('/upload', upload.single('uploaded_file'), (req, res) => {
res.sendStatus(200)
})

app.listen(PORT, () => console.log(`multer example listening on ${PORT}!`))
17 changes: 17 additions & 0 deletions examples/FormDataBlob/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "FormData",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^5.1.0",
"multer": "^2.3.0"
}
}
43 changes: 43 additions & 0 deletions examples/FormDataBlob/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html>
<head>
<title>Multer Example</title>
</head>
<body>

<label>Enter text to upload:</label>
<input type="text" id="the-text" />
<button id="multer-test">send data with FormData</button>

<script type="text/javascript">
const postData = () => {

const theText = document.getElementById('the-text').value;
const formData = new FormData();
const blob = new Blob([theText], { type: 'text/plain' });
formData.append('uploaded_file', blob);

const options = {
method: 'POST',
body: formData
};

fetch('http://localhost:3000/upload', options)
.then(res => {
if (res.ok) {
return res.text()
}
return Promise.reject(res.statusText);
})
.then(() => alert('upload successful!'))
.catch(err => {
const errorText = 'error uploading';
console.error(`${errorText}: ${err}`);
alert(errorText);
});
};

document.getElementById('multer-test').addEventListener('click', postData);
</script>
</body>
</html>
2 changes: 2 additions & 0 deletions examples/FormDataImage/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
public/data/uploads/
3 changes: 3 additions & 0 deletions examples/FormDataImage/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## FormDataImage

uses an `<image type="file" />` tag with the `Image` constructor to demonstrate sending an image to the multer middleware on the server.
15 changes: 15 additions & 0 deletions examples/FormDataImage/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const express = require('express')
const app = express()
const multer = require('multer')

const PORT = 3000

const upload = multer({ dest: './public/data/uploads/' })

app.use('/', express.static('public'))

app.post('/upload', upload.single('uploaded_file'), (req, res) => {
res.sendStatus(200)
})

app.listen(PORT, () => console.log(`multer example listening on ${PORT}!`))
17 changes: 17 additions & 0 deletions examples/FormDataImage/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "FormData",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^5.1.0",
"multer": "^2.3.0"
}
}
44 changes: 44 additions & 0 deletions examples/FormDataImage/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<!DOCTYPE html>
<html>
<head>
<title>Multer Example</title>
</head>
<body>

<label>Choose a file to upload:</label>
<input type="file" id="the-file" />
<button id="multer-test">send image with FormData</button>

<script type="text/javascript">
const postData = () => {

const fileInputNode = document.getElementById('the-file');

const formData = new FormData();

formData.append('uploaded_file', fileInputNode.files[0]);

const options = {
method: 'POST',
body: formData
};

fetch('http://localhost:3000/upload', options)
.then(res => {
if (res.ok) {
return res.text()
}
return Promise.reject(res.statusText);
})
.then(() => alert('upload successful!'))
.catch(err => {
const errorText = 'error uploading';
console.error(`${errorText}: ${err}`);
alert(errorText);
});
};

document.getElementById('multer-test').addEventListener('click', postData);
</script>
</body>
</html>