| 123456789101112131415161718192021222324252627282930313233343536 |
- 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 extname = path.extname(filename);
-
- const shortPath = id + (extname || '');
- const fullPath = filePath + path.sep + shortPath;
- fs.rename(oldpath, fullPath, err => {
- if (err) {
- reject(err);
- }else {
- resolve({
- id: id,
- name: filename,
- path: shortPath,
- size: size
- })
- }
- })
- })
- });
- }
|