node.js实现图片上传(包含缩略图)
生活随笔
收集整理的這篇文章主要介紹了
node.js实现图片上传(包含缩略图)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
圖片上傳
使用multiparty插件實(shí)現(xiàn)上傳
安裝multiparty
npm i --save multiparty代碼實(shí)現(xiàn)
const multiparty = require('multiparty'); let form = new multiparty.Form({uploadDir: upload.path});構(gòu)造參數(shù)說(shuō)明
- encoding 設(shè)置接收數(shù)據(jù)編碼,默認(rèn)是utf-8
- maxFieldsSize 限制字段可以分配的內(nèi)存量,默認(rèn)2M
- maxFields 限制在發(fā)出錯(cuò)誤事件之前將要解析的字段數(shù),默認(rèn)1000
- maxFilesSize 限制總文件大小,默認(rèn)無(wú)窮大
- autoFields 啟用字段事件并禁用字段的部分事件。如果添加字段偵聽器,則自動(dòng)將其設(shè)置為true。
- autoFiles 啟用文件事件并禁用文件的部分事件。如果添加了一個(gè)文件偵聽器,則自動(dòng)將其設(shè)置為true。
- uploadDir 文件上傳的目錄
==如果回調(diào)提供,autofields和autofiles被設(shè)置為true,所有字段和文件的收集和傳遞給回調(diào),不再需要聽任何形式的事件。==
事件說(shuō)明
- part 請(qǐng)求文件數(shù)據(jù)時(shí)觸發(fā),回調(diào)函數(shù)是一個(gè)實(shí)現(xiàn)可讀流的實(shí)例對(duì)象
- headers:頭部文件
- name:字段名稱
- filename:文件名稱
- byteFffset:主體數(shù)據(jù)的字節(jié)偏移量
- byteCount:數(shù)據(jù)總的字節(jié)長(zhǎng)度
aborted 在請(qǐng)求中止時(shí)觸發(fā)
close 在請(qǐng)求結(jié)束之后觸發(fā)
- file 接收到文件的參數(shù)
- name:字段名稱
- file:存儲(chǔ)著文件信息的對(duì)象
- fieldName:字段名稱
- originalFilename:文件名稱
- path:寫到磁盤上文件的具體路徑
- headers:存儲(chǔ)著頭部信息
- size:文件具體大小
- field 獲取請(qǐng)求的具體數(shù)據(jù)。回調(diào)函數(shù)兩個(gè)參數(shù)
- name:字段名
- value:字段值
==注意使用part事件時(shí),如果同時(shí)監(jiān)聽fields和files事,part事件會(huì)獲取不到數(shù)據(jù)。==
更多說(shuō)明
增加事件監(jiān)聽后
let upload = uploadPath();console.dir(upload);let form = new multiparty.Form({uploadDir: upload.path});form.on('error', function (err) {console.log('Error parsing form: ' + err.stack);});form.on('file', (name, file) => {console.log(file);res.json(file);});form.on('close', function () {console.log('Upload completed!');});form.parse(req);圖片處理
一般來(lái)說(shuō)上傳圖片都會(huì)進(jìn)行簡(jiǎn)單的處理,例如無(wú)損畫質(zhì)壓縮,縮略圖生成等
1、用 resize-img 進(jìn)行縮略圖制作
安裝組件
npm install --save resize-img
代碼實(shí)現(xiàn)
const resizeImg = require('resize-img');resizeImg(fs.readFileSync(file_path), {width: 800}).then(buf => {fs.writeFileSync(file_path, buf);});2、使用python圖片處理庫(kù)PIL
為什么使用python?
CPU密集型任務(wù)是Node.js的軟肋,當(dāng)服務(wù)器同時(shí)執(zhí)行多個(gè)圖片處理時(shí)(特別是比較大的圖片時(shí)),會(huì)出現(xiàn)BUG,所以我們可以選用python圖片處理庫(kù)PIL
PIL安裝
pip install pillowpython實(shí)現(xiàn)
from PIL import Image import glob import os import systry:im = Image.open(sys.argv[1])o_width = im.size[0]o_height = im.size[1]thumb_width = 400size = (thumb_width, o_height * thumb_width / o_width)im.thumbnail(size)im.save(sys.argv[2],im.format)print(sys.argv[2]) except IOError:print("cannot create thumbnail for", sys.argv[1])node調(diào)用pyhton
const path = require('path'); const exec = require('child_process').exec;let baseDir = path.resolve(__dirname, '..');let thumb_pic = (arg1, arg2) => {return new Promise((resolve, reject) => {let py_path = baseDir + "/public/py/";exec(`python ${py_path}thumb_pic.py ${arg1} ${arg2}`, function (error, stdout, stderr) {if (stdout.length > 0) {console.log('you offer args:', stdout);resolve(true);} else {console.log('you don\'t offer args');resolve(false);}if (error) {console.info('stderr : ' + stderr);reject(stderr);}});}); };module.exports.thumb_pic = thumb_pic;這里我推薦使用第二種方法
源碼地址
轉(zhuǎn)載于:https://www.cnblogs.com/youjun1991/p/8664171.html
總結(jié)
以上是生活随笔為你收集整理的node.js实现图片上传(包含缩略图)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 爬虫工具获取页面中域名及子域名(SQL注
- 下一篇: 挖矿病毒解决实例(隐藏进程,文章较好)(