版權聲明:本文為部落客原創文章,未經部落客允許不得轉載。 https://blog.csdn.net/qq1010885678/article/details/37519271
在前台配置完CKEditor和SWFUpload之後就可以滿足基本的需求了
在這裡,我配置的接收異步上傳的圖檔的頁面為upload.ashx
在這個ashx中對上傳的圖檔處理的流程如下:
context.Response.ContentType = "text/plain";
HttpPostedFile file = context.Request.Files["Filedata"];//接收到上傳的圖檔
string fileName = string.Empty;
string fileExtension = string.Empty;
if (file != null)
{
fileName = Path.GetFileName(file.FileName);//擷取圖檔名
fileExtension = Path.GetExtension(file.FileName);//擷取擴充名
}
if (fileExtension == ".jpg")
{
string saveDir = "/upload/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/";//根據目前年月日在upload檔案夾中建立該圖檔的儲存路徑,便于管理
Directory.CreateDirectory(Path.GetDirectoryName(context.Server.MapPath(saveDir)));//建立路徑
string fullDir = saveDir + GetMD5.GetFileMD5(file.InputStream) + fileExtension;//使用圖檔的md5作為該圖檔儲存的名字
file.SaveAs(context.Server.MapPath(fullDir));
context.Response.Write("ok;" + fullDir);//儲存完畢之後将圖檔的路徑傳回
}
需要注意的幾點是:
1.在伺服器端儲存檔案一定要用絕對路徑,如context.Server.MapPath(fullDir)
2.使用圖檔的md5值作為圖檔的名字,一來保證圖檔名不會重複,二來在使用者上傳同一張照片的時候可以将其覆寫
最後,功能比較完善的Ubb編輯器誕生了~~
不過由于編輯器可以轉成源碼模式
使用者還是可以直接在源碼模式下輸入<,>這些符号
是以在背景接收使用者輸入的資料的時候
msg = msg.Replace("<", "<").Replace(">", ">");
需要對<,>進行替換
最後的最後~
在向使用者展示資訊的時候,需要把ubb代碼轉換成html,這樣浏覽器才能解析
提供一個寫好的靜态類,要用的時候将ubb代碼傳進去,傳回的就是html代碼
UbbHelper裡面有挺多的問題,需要根據自己的需求更改代碼
至此,大功告成!