說(shuō)明
作為一個(gè)node 初學(xué)者,最近在做一個(gè)聊天軟件,支持注冊(cè)、登錄、在線(xiàn)單人、多人聊天、表情發(fā)送、各種文件上傳下載、增刪好友、聊天記錄保存、通知聲開(kāi)關(guān)、背景圖片切換、游戲等功能,所以用到了multer 模塊,經(jīng)過(guò)各種查文檔,做demo例子,終于成功實(shí)現(xiàn)單個(gè)文件上傳功能,支持大部分文件格式上傳,同時(shí)顯示到網(wǎng)頁(yè)上
效果
是不是有種微信即視感,沒(méi)錯(cuò),就是根據(jù)網(wǎng)頁(yè)版微信來(lái)做的,
要實(shí)現(xiàn)整體效果的話(huà),要配合css和html來(lái)做,前端初學(xué)者,第一次發(fā)博客,實(shí)在捉急,近期,將會(huì)將代碼放到github上去,感興趣的朋友可以去看一下
下面直接上代碼,輕虐
配置
安裝
直接通過(guò)cmd命令窗口安裝multer
npm install multer -save
服務(wù)器代碼
//引入http const http=require("http"); //引入express const express=require("express"); //引入multer const multer=require("multer"); //創(chuàng)建服務(wù)器,綁定監(jiān)聽(tīng)端口 var app=express(); var server=http.createServer(app); server.listen(8081); //建立public文件夾,將HTML文件放入其中,允許訪(fǎng)問(wèn) app.use(express.static("public")); //文件上傳所需代碼 //設(shè)置文件上傳路徑和文件命名 var storage = multer.diskStorage({ destination: function (req, file, cb){ //文件上傳成功后會(huì)放入public下的upload文件夾 cb(null, './public/upload') }, filename: function (req, file, cb){ //設(shè)置文件的名字為其原本的名字,也可以添加其他字符,來(lái)區(qū)別相同文件,例如file.originalname+new Date().getTime();利用時(shí)間來(lái)區(qū)分 cb(null, file.originalname) } }); var upload = multer({ storage: storage }); //處理來(lái)自頁(yè)面的ajax請(qǐng)求。single文件上傳 app.post('/upload', upload.single('file'), function (req, res, next) { //拼接文件上傳后的網(wǎng)絡(luò)路徑, var url = 'http://' + req.headers.host + '/upload/' + req.file.originalname; //將其發(fā)回客戶(hù)端 res.json({ code : 1, data : url }); res.end(); });
客戶(hù)端代碼
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <!--依托于jquery--> <script src="js/jquery-1.11.3.js"></script> </head> <body> <div class="container"> <label>file</label> <input type="file" name="file" id="file"> </div> <script> var file=$("#file")[0]; //這里使用的是onchange事件,所以當(dāng)你選擇完文件之后,就觸發(fā)事件上傳 file.onchange=function(){ //創(chuàng)建一個(gè)FormDate var formData=new FormData(); //將文件信息追加到其中 formData.append('file',file.files[0]); //利用split切割,拿到上傳文件的格式 var src=file.files[0].name, formart=src.split(".")[1]; //使用if判斷上傳文件格式是否符合 if(formart=="jpg"||formart=="png"|| formart=="docx"||formart=="txt"|| formart=="ppt"||formart=="xlsx"|| formart=="zip"||formart=="rar"|| formart=="doc"){ //只有滿(mǎn)足以上格式時(shí),才會(huì)觸發(fā)ajax請(qǐng)求 $.ajax({ url: '/upload', type: 'POST', data: formData, cache: false, contentType: false, processData: false, success: function(data){ //上傳成功之后,返回對(duì)象data if(data.code>0){ var src=data.data; //利用返回值src 網(wǎng)絡(luò)路徑,來(lái)實(shí)現(xiàn)上傳文檔的下載 if(formart=="docx"||formart=="txt"||formart=="doc"){ //結(jié)合css樣式,實(shí)現(xiàn)顯示圖標(biāo) var className="docx"; //拼接html,生成到頁(yè)面上去 var msg=`<a href="${src}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><i class="${className}"></i></a>`; }else if(formart=="ppt"){ //PPT 格式 className="ppt"; msg=`<a href="${src}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><i class="${className}"></i></a>`; }else if(formart=="xlsx"){ //xlsx 格式 className="xlsx"; msg=`<a href="${src}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><i class="${className}"></i></a>`; }else if(formart=="zip"||formart=="rar"){ //zip || rar 格式 className="zip"; msg=`<a href="${src}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><i class="${className}"></i></a>`; }else{ //所有的圖片格式 msg=`<a href="javascript:;" rel="external nofollow" class="picCheck"><img src="${src}"></a>`; } // 這里將msg 追加到你要顯示的區(qū)域 } } //不滿(mǎn)足上傳格式時(shí) }else{ alert("文件格式不支持上傳") } } </script> </body> </html>
總結(jié)
以上所述是小編給大家介紹的Ajax 配合node js multer 實(shí)現(xiàn)文件上傳功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com