目前正在學習mvc,正在做一個個人博客的網站,其中有關於文章的操作,所以加上了富文本編輯器。我目前使用的編輯器是ckeditor3.6.4+ckfinder2.2.2。
今天用編輯器上傳圖片,弄了N久。發現上傳圖片的時候,就直接上傳到服務器,最終提交內容時候,就把內容直接提交。但是總有那么種情況,萬一內容提交不成功,那之前上傳的圖片不就在服務器成了垃圾了么。但是這個問題目前還沒解決,求高人指點,在什么時候掃描一下BaseUrl = "<圖片上傳時,圖片存放的位置>";!然后清除這些無用的圖片。
在網上百度了,說ckfinder可以修改config.ascx中的BaseUrl來修改上傳圖片存放的位置,但是我想動態的、根據不同情況來使上傳的圖片放在不同的位置。然后去弄弄了ckfinder,沒有文檔,看得暈頭轉向的。心想,自己寫段代碼來操作服務器上的上傳文件算了。
目前僅僅是解決了將上傳的圖片存放到指定的目錄,並且修改ckeditor內容中的圖片鏈接。對於多用戶的個人網站來說,可能需要將用戶的圖片分類存放,而且還要改名(還沒有什么大型個人網站的開發經驗,也不知道自己的想法對不對,求各位前輩幫忙指正)。
先上代碼,懇請各位幫忙指出不足。拜謝。。
action:Default

1 [HttpPost] 2 public ActionResult Default(Article article) 3 { 4 string ss = article.ArticleContent; 5 6 //新文件夾的路徑 7 string filenewpath = Request.PhysicalApplicationPath + "Content\\ss\\"; 8 9 ss = RepleaceImg(ss, filenewpath); 10 11 Response.Write(ss); 12 13 return Content("<script language=\"javascript\">{alert(\"sss\");location.href=\"/Home/Default\";}</script>"); 14 //有點懶,沒有單獨做個頁面用於顯示文章內容,當時就想快些調試好。 15 }
這里說一下,Article是自定義的一個類,屬性ArticleContent用了[skipValidation]。由於目前學得還不深,我不會設置安全驗證,如果在Default中使用FormCollection ,加上[validationInput(false)],在web.config中設置<httpRuntime>也試過,一直說我提交的Request.Form[""]存在安全隱患。求前輩們給些指點。。RepleaceImg()是自定義函數,用於替換ArticleContent中<img >的src。
RepleaceImg的代碼

1 private string RepleaceImg(string content, string filenewpath) 2 { 3 //新文件名列表 4 List<string> lsImageNewName = new List<string>(); 5 //暫存文件名列表 6 List<string> lsImageNameTemp = new List<string>(); 7 //ArticleContent中的圖片地址列表,將要替換的文件名 8 List<string> lsImageOleName = new List<string>(); 9 10 //將要替換的文件名 11 string filename = ""; 12 //文件上傳的路徑 13 string filepath = ""; 14 //將要移動到的文件目錄路徑 15 //string filenewpath = ""; 16 //新文件名 17 string filenewname = ""; 18 //獲取上傳文件的路徑,含文件名以及擴展名 19 string temp = ""; 20 21 //文章內容中有圖片 22 if (content.IndexOf("src=\"") != -1) 23 { 24 GetFilePath(content, lsImageOleName); 25 } 26 27 //上傳的圖片所存放的位置 28 filepath = Request.PhysicalApplicationPath + "Content\\images\\images"; 29 30 if (!Directory.Exists(filenewpath)) 31 { 32 Directory.CreateDirectory(filenewpath); 33 } 34 //移動filepath中圖片到filenewpath中。並且修改圖片的文件名 35 filenewname = ""; 36 if (Directory.Exists(filepath)) 37 { 38 foreach (string d in Directory.GetFileSystemEntries(filepath)) 39 { 40 if (System.IO.File.Exists(d)) 41 { 42 string strExtension = System.IO.Path.GetExtension(d); 43 filenewname = filenewpath + DateTime.Now.ToString("yyyymmddhhssmmfff"); 44 System.IO.File.Move(d, filenewname + strExtension); 45 lsImageNameTemp.Add(filenewname + strExtension); 46 } 47 } 48 } 49 50 //替換,將文件圖片的路徑改成,<img>能夠識別的路徑 51 filenewname = ""; 52 foreach (string m in lsImageNameTemp) 53 { 54 filenewname = "http://www.cnblogs.com/Content/ss/" + m.Substring(m.LastIndexOf("\\") + 1, m.Length - m.LastIndexOf("\\") - 1); 55 lsImageNewName.Add(filenewname); 56 } 57 58 int count = 0; 59 foreach (string nametemp in lsImageOleName) 60 { 61 try 62 { 63 content = content.Replace(nametemp, lsImageNewName[count]); 64 } 65 catch (Exception ex) 66 { 67 break; 68 } 69 finally 70 { 71 count++; 72 } 73 74 } 75 76 return content; 77 }
GetFilePath的代碼

1 private void GetFilePath(string content,List<string> lsImageName) 2 { 3 string temp = ""; 4 5 if (content.IndexOf("src=\"") != -1) 6 { 7 temp = content.Substring(content.IndexOf("src=\"") + 5, content.IndexOf("jpg\"") - content.IndexOf("src=\"") - 2); 8 lsImageName.Add(temp); 9 10 //截取剩下的字符串 11 string qqq = content.Substring(content.IndexOf("jpg\"") + 4); 12 GetFilePath(qqq, lsImageName); 13 } 14 else 15 { 16 return; 17 } 18 }
在編輯這段代碼的時候就沒有考慮全面,后來回想起,還有種情況:如果內容中出現這樣的情況改怎么辦
<script src=\"xxxx\">{document.GetElementById(\"img\").src=\"xxxx.jpg\"}</script>,但沒有<img src=\"xxxx.jpg\">。
如果是這樣的情況,這段代碼也會認為內容中存在圖片,處理起來應該會出現錯誤。懇請各位在百忙中抽那么一小點時間,給菜鳥我些指點。拜謝。。
如果各位對這段代碼有疑問的,可以盡管提出來,我會嘗試去回答。
2012-08-10
白天的時候進行了嘗試,當在ckeditor中輸入<script src=\"xxxx\">{document.GetElementById(\"img\").src=\"xxxx.jpg\"}</script>的時候,ckeditor會將這段代碼給編碼,里面的"<"、">"、"""都分別轉成<、>、"。這樣string.indexof("src=\"")應該只會尋找<img>中的src了。
另外,在RepleaceImg 函數中重新修改了下代碼。在if(!content.indexof("src=\"")!=-1)的時候才執行后續代碼,如不哦沒有,則直接返回content。ps:在自己的電腦上編輯的,但是現在沒有將源碼拷貝出來,所有沒有粘上最新代碼,也沒有附上實驗結果,不能很好的說明結果,請見諒。。