Some options is borrowed from jquery.upload
var Upload = require('upload-file');
npm install upload-file --save
app.post('/upload', function(req, res) {
var upload = new Upload({
dest: 'dest/path',
maxFileSize: 100 * 1024,
acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
rename: function(name, file) {
console.log(this.fields);
return file.filename;
}
});
upload.on('end', function(fields, files) {
if (!fields.channel) {
this.cleanup();
this.error('Channel can not be empty');
return;
}
res.send('ok')
});
upload.on('error', function(err) {
res.send(err);
});
upload.parse(req);
});
- {object}
options
required- {string}
options.dest
required
Upload path - {RegExp}
options.acceptFileTypes
The regular expression for allowed file types, matches against either file type or file name - {function}
options.rename(name, file)
Return filename for change the filename or path, name is the input file name, and file is a object - {number}
[options.maxNumberOfFiles=Infinity]
The limit of files to be uploaded - {number}
[options.minNumberOfFiles=1]
The minimum of files to be uploaded - {number}
[options.maxFileSize=Infinity]
The maximum allowed file size in bytes - {number}
[options.minFileSize=0]
The minimum allowed file size in bytes - {object}
options.messages
Error and info messages- message.maxNumberOfFiles
- message.minNumberOfFiles
- message.acceptFileTypes
- message.maxFileSize
- message.minFileSize
- message.invalidRequest
- {string}
Inherit from node's EventEmitter
Parse the http request
If the form is invalid, you can invoke this method for clear unnecessary file
Emit error event with msg
The fields of form, it's a object
{
name: 'value',
city: ['1', '2']
}
The files of form, it's a obejct
{
file: {
filename: 'index.css',
path: 'upload/path/index.css',
type: 'text/css',
size: 100
},
files: [{...}, {...}]
}
// If the file.filename is empty string,
// it's means that user do not upload file with this file input
- cd
upload-file
modules path - npm install
- node express.js