| 123456789101112131415161718192021222324252627282930313233 |
- const formidable = require('formidable');
- const fs = require('fs');
- const path = require('path');
- const Entities = require('html-entities').XmlEntities;
- const entities = new Entities();
- const filePath = path.resolve(__dirname, '..' + path.sep + 'upload');
- module.exports = function(request, response){
- const form = new formidable.IncomingForm();
- return new Promise((resolve, reject) => {
- form.parse(request, (err, fields, files) => {
- const file = files.file;
- const oldpath = file.path;
- const filename = file.name;
- const size = file.size;
- const id = Date.now().toString(36) + Math.random().toString(36).substr(3);
-
- const newpath = filePath + path.sep + entities.decode(filename);
- fs.rename(oldpath, newpath, err => {
- if (err) {
- reject(err);
- }else {
- resolve({
- id: id,
- name: filename,
- path: newpath,
- size: size
- })
- }
- })
- })
- });
- }
|