Ueditor官方文件之上傳路徑配置
文件說明
支援版本:支援 UEditor 1.4.2+ 的版本
1.4.2+ 以後路徑配置主要相關的配置項是 PathFormat 和 UrlPrefix 的配置項。
這兩個配置項主要針對如下功能:
- 圖片上傳:imagePathFormat、imageUrlPrefix
- 塗鴉上傳:scrawlPathFormat、scrawlUrlPrefix
- 截圖上傳:snapscreenPathFormat、snapscreenUrlPrefix
- 附件上傳:filePathFormat、fileUrlPrefix
- 視訊上傳:videoPathFormat、videoUrlPrefix
當前文件的例子是以 圖片上傳
imagePathFormat 介紹
作用:指定檔案上傳路徑和返回路徑,支援格式化
1.4.2+ 路徑配置項無論是否以 "/" 開頭,都是相對於 網站根目錄 的路徑。
例如,假設網站根目錄是:"D://apache/www/",以下是 imagePathFormat 的配置值以及對應的存放目錄。
"/upload/{filename}" --> "D://apache/www/upload/"
"upload/{filename}" --> "D://apache/www/upload/"
"./upload/{filename}" --> "D://apache/www/upload/"
"../upload/{filename}" --> "D://apache/upload/"
1 指定檔案儲存目錄
上傳路徑可以使用根路徑和相對路徑,推薦使用根路徑的配置。
1.1 使用 "/" 開頭的根路徑
imagePathFormat 引數推薦使用 "/" 開頭的配置,這樣的值指相對於網站根目錄的位置。
例子:網站根目錄是 "D://apache/www/",imagePathFormat 引數設定為 "/upload/image/{filename}",那麼上傳位置在 "D://apache/www/upload/image/" 目錄下。
1.2 使用非 "/" 開頭的相對路徑
imagePathFormat
例子1:網站根目錄是 "D://apache/www/",imagePathFormat 引數設定為 "ueditor/php/upload/{filename}",那麼上傳位置在 "D://apache/www/ueditor/php/upload/" 目錄下。
例子2:網站根目錄是 "D://apache/www/",imagePathFormat 引數設定為 "../upload/{filename}",那麼上傳位置在 "D://apache/upload/" 目錄下。
2 控制插入到編輯器的路徑
2.1 後臺返回路徑
後臺執行上傳結束後,返回路徑是按照 imagePathFormat 配置項替換後的字串,原樣輸出到前端。
例子1:上傳檔名為 123.jpg,imagePathFormat 引數設定為 "/ueditor/php/upload/{filename}",那麼返回路徑是 "/ueditor/php/upload/123.jpg"。
例子2:上傳檔名為 123.jpg,imagePathFormat 引數設定為 "../upload/{filename}",那麼返回路徑是 "../upload/123.jpg"。
2.2 通過 imageUrlPrefix 配置項給返回路徑新增字首
有一些情況下僅僅靠返回路徑是不能得到正常的圖片連結,需要通過配置 imageUrlPrefix 給插入圖片的路徑新增字首。
有兩種情況下需要配置 imageUrlPrefix:
-
asp和.net下,應用程式目錄不是網站根目錄,需要給路徑新增字首。
-
在跨域上傳的情況下,就需要配置imageUrlPrefix字首。假設頁面在 a.com 域下,檔案上傳到 b.com 域下,這樣要配置 imageUrlPrefix 為 "http://b.com" 才能得到正常路徑。
3 格式化上傳路徑和檔名
由於上傳檔名容易衝突,編輯器提供了配置上傳檔案路徑和檔名格式的方法,可以在 config.json 配置 imagePathFormat 項,後臺上傳檔案會按照配置的格式命名。
3.1 格式化字串的引數
{filename}//會替換成檔名 [要注意中文檔案亂碼問題]{rand:6}//會替換成隨機數,後面的數字是隨機數的位數{time}//會替換成時間戳{yyyy}//會替換成四位年份{yy}//會替換成兩位年份{mm}//會替換成兩位月份{dd}//會替換成兩位日期{hh}//會替換成兩位小時{ii}//會替換成兩位分鐘{ss}//會替換成兩位秒
3.4 檔名衝突
當按照模板命名檔案,依舊出現衝突,會直接覆蓋同名檔案,所以建議 imagePathFormat 使用時間戳和隨機數來較少衝突。
3.5 非法字元
後臺會過濾模板上的非法字元,非法字元列表如下,會替換成空:
\ :*?" < > |
4 完整的使用例子
4.1 例子一
網站根目錄是:"D://apache/www/" 配置項 imagePathFormat 的值為:
imagePathFormat ="/ueditor/php/upload/image/{yyyy}-{mm}-{dd}_{rand:6}_{filename}"上傳的檔名可能是這樣:
"2014-06-13_712623_照片.jpg"
存放的路徑是:
D://apache/www/ueditor/php/upload/image
4.1 例子二
假設例子的情景如下:
網站根目錄是:"D://apache/www/"
上傳檔名稱是:"123.jpg"
上傳日期是:2014年06月13日
配置項 imagePathFormat 和 imageUrlPrefix 的值為:
imagePathFormat:"/ueditor/php/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}",
imageUrlPrefix:"http://a.com"
那麼上傳結果可能是這樣:
上傳檔案命名為 "1402637667260888888.jpg"
檔案上傳到 "D://apache/www/ueditor/php/upload/image/20140613/" 目錄下
後端返回給前端的圖片連結是:
"/ueditor/php/upload/image/20140613/402637667260888888.jpg"
插入到編輯器的圖片連結是:
"http://a.com/ueditor/php/upload/image/20140613/402637667260888888.jpg"