使用node.js怎么實現(xiàn)多文件上傳?針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
創(chuàng)新互聯(lián)是專業(yè)的南票網(wǎng)站建設(shè)公司,南票接單;提供成都網(wǎng)站建設(shè)、做網(wǎng)站,網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行南票網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊,希望更多企業(yè)前來合作!
// 前端 upload.html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>上傳文件demo</title> <style media="screen"> .progress{ width: 50%; height: 5px; border: 1px solid #ccc; border-radius: 4px; margin-top: 10px; position: relative; } .progress>span{ display: inline-block; position: absolute; border-radius: 4px; top: 0; left: 0; height: 100%; width: 0; background-color: rgb(98, 230, 74); transition: width 0.3s ease-out; } </style> </head> <body> <input id="file" type="file" multiple> <div class="progress"> <span></span> </div> <script type="text/javascript"> var http = function (option) { // 過濾請求成功后的響應(yīng)對象 function getBody (xhr) { var text = xhr.responseText || xhr.response if (!text) { return text } try { return JSON.parse(text) } catch (err) { return text } } var xhr = new XMLHttpRequest(); // 自定義 beforeSend 函數(shù) if(option.beforeSend instanceof Function) { if (option.beforeSend(xhr) === false) { return false } } xhr.onreadystatechange = function () { if (xhr.status === 200) { if (xhr.readyState === 4) { // 成功回調(diào) option.onSuccess(getBody(xhr)) } } } // 請求失敗 xhr.onerror = function (err) { option.onError(err) } xhr.open(option.type, option.url, true) // 當(dāng)請求為上傳文件時回調(diào)上傳進(jìn)度 if (xhr.upload) { xhr.upload.onprogress = function (event) { if (event.total > 0) { event.percent = event.loaded / event.total * 100; } // 監(jiān)控上傳進(jìn)度回調(diào) if (option.onProgress instanceof Function) { option.onProgress(event) } } } // 自定義頭部 const headers = option.headers || {} for (var item in headers) { xhr.setRequestHeader(item, headers[item]) } xhr.send(option.data) } // 測試接口 http({ type: 'POST', url: '/test', data: JSON.stringify({ name: 'yolo' }), onSuccess: function (data) { console.log(data) }, onError: function (err) { console.log(err) } }) document.getElementById('file').onchange = function () { var fileList = this.files, formData = new FormData(); Array.prototype.forEach.call(fileList, function (file) { formData.append(file.name, file) }) // 當(dāng)上傳的數(shù)據(jù)為 file 類型時,請求的格式類型自動會變?yōu)?nbsp;multipart/form-data, 如果頭部格式有特定需求,在我的 http 函數(shù)中傳入 headers<Object> 即可,大家可自己查看,我這里沒有什么特殊處理所以就不傳了 http({ type: 'POST', url: '/upload', data: formData, onProgress: function (event) { console.log(event.percent) document.querySelector('.progress span').style.width = event.percent + '%'; }, onSuccess: function (data) { console.log('上傳成功') }, onError: function (err) { alert(err) } }) } </script> </body> </html>
后端所用的一些東西我放在這
express中間件-multer
express 4.x 文檔
// 后端(node.js) upload.js var express = require('express'); var path = require('path'); var fs = require('fs'); var app = express(); var bodyParser = require('body-parser'); // 過濾請求頭部相應(yīng)格式的body var multer = require('multer'); var chalk = require('chalk'); // 只是一個 cli 界面字體顏色包而已 var log = console.log.bind(console); app.use(express.static('static')); // 接受 application/json 格式的過濾器 var jsonParser = bodyParser.json() // 接受 application/x-www-form-urlencoded 格式的過濾器 var urlencodedParser = bodyParser.urlencoded({ extended: false }) // 接受 text/html 格式的過濾器 var textParser = bodyParser.text() // 自定義 multer 的 diskStorage 的存儲目錄與文件名 var storage = multer.diskStorage({ destination: function (req, file, cb) { cb(null, 'view') }, filename: function (req, file, cb) { cb(null, file.fieldname) } }) var upload = multer({ storage: storage }) // 頁面渲染 app.get('/', function (req, res) { res.sendFile(path.join(__dirname, 'view/upload.html')); }) app.post('/test', textParser, jsonParser, function (req, res) { log(req.body); var httpInfo = http.address(); res.send({ host: httpInfo.address, port: httpInfo.port }) }) // 對應(yīng)前端的上傳接口 http://127.0.0.1:3000/upload, upload.any() 過濾時不對文件列表格式做任何特殊處理 app.post('/upload', upload.any(), function (req, res) { log(req.files) res.send({message: '上傳成功'}) }) // 監(jiān)控 web 服務(wù) var http = app.listen(3000, '127.0.0.1', function () { var httpInfo = http.address(); log(`創(chuàng)建服務(wù)${chalk.green(httpInfo.address)}:${chalk.yellow(httpInfo.port)}成功`) })
關(guān)于使用node.js怎么實現(xiàn)多文件上傳問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。
分享名稱:使用node.js怎么實現(xiàn)多文件上傳
網(wǎng)站網(wǎng)址:http://www.muchs.cn/article24/ghgcce.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、、自適應(yīng)網(wǎng)站、App開發(fā)、建站公司、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)