egg-ueditor,基於egg的UEditor百度編輯器後端實現,支援圖片/檔案上傳、列表及圖片遠端抓取
阿新 • • 發佈:2018-12-25
egg-ueditor
基於egg的UEditor百度編輯器後端實現,支援圖片/檔案上傳、列表及圖片遠端抓取
原始碼:https://github.com/inmyjs/egg-ueditor
安裝
npm install egg-ueditor --save
使用方法
- 在app/router.js中如下使用
const ueditor = require('egg-ueditor')
app.all('/ueditor', ueditor())
注意:預設上傳至egg靜態資源目錄app/public,若需要上傳至其他目錄,如下使用:
app. all('/ueditor', ueditor(['assets/public','public']))
引數為一個數組,第1個引數為靜態資源相對目錄,第2個引數為靜態資源url對映路徑。
返回的圖片/檔案url為完整http地址,以方便前後端分佈部署,比如:http://127.0.0.1:7001/public/upload/ueditor/image/20181223/1545560529310279850.png
- 可以修改 UEditor 配置,具體的引數請參考 UEditor 官方的文件
// 需要傳UEditor 配置物件
router.all('/ueditor', ueditor({
"imageAllowFiles" : [".png", ".jpg", ".jpeg"]
"imagePathFormat": "/upload/ueditor/image/{yyyy}{mm}{dd}/{filename}" // 儲存為原檔名
}))
或者
// 需要傳UEditor 配置物件
router.all('/ueditor', ueditor(['app/public','public'],{
"imageAllowFiles": [".png", ".jpg", ".jpeg"]
"imagePathFormat": "/upload/ueditor/image/{yyyy}{mm}{dd}/{filename}" // 儲存為原檔名
}))
注意
由於egg對post預設有CSRF 校驗,所以前端的上傳地址需要這樣寫:
serverUrl: “/ueditor?_csrf={{ ctx.csrf | safe }}”
如果前後端是分離的,那就這樣寫:
let csrf = $.cookies.get(‘csrfToken’);
serverUrl: “/ueditor?_csrf=”+csrf
詳細說明請檢視 https://eggjs.org/zh-cn/core/security.html
非常感謝您的支援
擼碼不易,如果對你有所幫助,歡迎您的讚賞!微信讚賞碼: