This commit is contained in:
Jonasz Bigda
2023-03-25 21:51:42 +01:00
parent 0db1d5117e
commit b332e9ceb0
1044 changed files with 37502 additions and 63938 deletions

68
node_modules/multer/README.md generated vendored
View File

@@ -9,9 +9,12 @@ on top of [busboy](https://github.com/mscdex/busboy) for maximum efficiency.
This README is also available in other languages:
- [Español](https://github.com/expressjs/multer/blob/master/doc/README-es.md) (Spanish)
- [简体中文](https://github.com/expressjs/multer/blob/master/doc/README-zh-cn.md) (Chinese)
- [한국어](https://github.com/expressjs/multer/blob/master/doc/README-ko.md) (Korean)
- [Русский язык](https://github.com/expressjs/multer/blob/master/doc/README-ru.md) (Russian)
- [Việt Nam](https://github.com/expressjs/multer/blob/master/doc/README-vi.md) (Vietnam)
- [Português](https://github.com/expressjs/multer/blob/master/doc/README-pt-br.md) (Portuguese Brazil)
## Installation
@@ -34,11 +37,11 @@ Don't forget the `enctype="multipart/form-data"` in your form.
```
```javascript
var express = require('express')
var multer = require('multer')
var upload = multer({ dest: 'uploads/' })
const express = require('express')
const multer = require('multer')
const upload = multer({ dest: 'uploads/' })
var app = express()
const app = express()
app.post('/profile', upload.single('avatar'), function (req, res, next) {
// req.file is the `avatar` file
@@ -50,7 +53,7 @@ app.post('/photos/upload', upload.array('photos', 12), function (req, res, next)
// req.body will contain the text fields, if there were any
})
var cpUpload = upload.fields([{ name: 'avatar', maxCount: 1 }, { name: 'gallery', maxCount: 8 }])
const cpUpload = upload.fields([{ name: 'avatar', maxCount: 1 }, { name: 'gallery', maxCount: 8 }])
app.post('/cool-profile', cpUpload, function (req, res, next) {
// req.files is an object (String -> Array) where fieldname is the key, and the value is array of files
//
@@ -65,16 +68,42 @@ app.post('/cool-profile', cpUpload, function (req, res, next) {
In case you need to handle a text-only multipart form, you should use the `.none()` method:
```javascript
var express = require('express')
var app = express()
var multer = require('multer')
var upload = multer()
const express = require('express')
const app = express()
const multer = require('multer')
const upload = multer()
app.post('/profile', upload.none(), function (req, res, next) {
// req.body contains the text fields
})
```
Here's an example on how multer is used an HTML form. Take special note of the `enctype="multipart/form-data"` and `name="uploaded_file"` fields:
```html
<form action="/stats" enctype="multipart/form-data" method="post">
<div class="form-group">
<input type="file" class="form-control-file" name="uploaded_file">
<input type="text" class="form-control" placeholder="Number of speakers" name="nspeakers">
<input type="submit" value="Get me the stats!" class="btn btn-default">
</div>
</form>
```
Then in your javascript file you would add these lines to access both the file and the body. It is important that you use the `name` field value from the form in your upload function. This tells multer which field on the request it should look for the files in. If these fields aren't the same in the HTML form and on your server, your upload will fail:
```javascript
const multer = require('multer')
const upload = multer({ dest: './public/data/uploads/' })
app.post('/stats', upload.single('uploaded_file'), function (req, res) {
// req.file is the name of your file in the form above, here 'uploaded_file'
// req.body will hold the text fields, if there were any
console.log(req.file, req.body)
});
```
## API
### File information
@@ -115,7 +144,7 @@ In an average web app, only `dest` might be required, and configured as shown in
the following example.
```javascript
var upload = multer({ dest: 'uploads/' })
const upload = multer({ dest: 'uploads/' })
```
If you want more control over your uploads, you'll want to use the `storage`
@@ -170,16 +199,17 @@ where you are handling the uploaded files.
The disk storage engine gives you full control on storing files to disk.
```javascript
var storage = multer.diskStorage({
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, '/tmp/my-uploads')
},
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now())
const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9)
cb(null, file.fieldname + '-' + uniqueSuffix)
}
})
var upload = multer({ storage: storage })
const upload = multer({ storage: storage })
```
There are two options available, `destination` and `filename`. They are both
@@ -207,14 +237,18 @@ the file (`file`) to aid with the decision.
Note that `req.body` might not have been fully populated yet. It depends on the
order that the client transmits fields and files to the server.
For understanding the calling convention used in the callback (needing to pass
null as the first param), refer to
[Node.js error handling](https://www.joyent.com/node-js/production/design/errors)
#### `MemoryStorage`
The memory storage engine stores the files in memory as `Buffer` objects. It
doesn't have any options.
```javascript
var storage = multer.memoryStorage()
var upload = multer({ storage: storage })
const storage = multer.memoryStorage()
const upload = multer({ storage: storage })
```
When using memory storage, the file info will contain a field called
@@ -274,8 +308,8 @@ If you want to catch errors specifically from Multer, you can call the
middleware function by yourself. Also, if you want to catch only [the Multer errors](https://github.com/expressjs/multer/blob/master/lib/multer-error.js), you can use the `MulterError` class that is attached to the `multer` object itself (e.g. `err instanceof multer.MulterError`).
```javascript
var multer = require('multer')
var upload = multer().single('avatar')
const multer = require('multer')
const upload = multer().single('avatar')
app.post('/profile', function (req, res) {
upload(req, res, function (err) {