實現FCKeditor 多用戶分文件夾上傳圖片等附件

導讀:
  本文原創轉載請註明出處
  項目需要在線HTML編輯器,就選擇了FCKeditor,目前最新是2.5Bate,不過穩定點定還是選了2.4.3,而.net的控件還是2.2沒變過 ,大概如何使用見我之前的“FCKeditor 2.3 在ASP.NET中文件上傳路徑的設置”,關於它的配置如:界面佈局啊什麼的網上去搜索下,太多了,就不寫了
  FCKeditor在web.config中有多項設置:





  用戶登錄後通過FCKeditor上傳文件則要放置在用戶共用上傳路徑“/Resources/UserUpload/”+“用戶郵箱地址”,如“/Resources/UserUpload/[email protected]”。FCKeditor.net獲取上傳路徑文件是:FileWorkerBase.cs,打開找到以下部分protected string UserFilesPath
{
get
{
if ( sUserFilesPath == null )
{
// 第一回從Application["FCKeditor:UserFilesPath"] 中讀取,如果沒有嘗試其它方式
sUserFilesPath = (string)Application["FCKeditor:UserFilesPath"] ;
// 第二回從Session["FCKeditor:UserFilesPath"] 中讀取,如果沒有嘗試其它方式
if ( sUserFilesPath == null || sUserFilesPath.Length == 0 )
{
sUserFilesPath = (string)Session["FCKeditor:UserFilesPath"] ;

// 第三回從web.config中讀取,如果沒有嘗試其它方式
if ( sUserFilesPath == null || sUserFilesPath.Length == 0 )
{
sUserFilesPath = System.Configuration.ConfigurationSettings.AppSettings["FCKeditor:UserFilesPath"] ;

// 第四回從DEFAULT_USER_FILES_PATH(這個變量在同文件中)中讀取,如果沒有嘗試其它方式
if ( sUserFilesPath == null || sUserFilesPath.Length == 0 )
sUserFilesPath = DEFAULT_USER_FILES_PATH ;
// 第五回從網址參數ServerPath中讀取
if ( sUserFilesPath == null || sUserFilesPath.Length == 0 )
{
sUserFilesPath = Request.QueryString["ServerPath"] ;
}
}
}
// Check that the user path ends with slash ("/")
if ( ! sUserFilesPath.EndsWith("/") )
sUserFilesPath += "/" ;
}
return sUserFilesPath ;
}
}  從上面的註釋可以看到用戶上傳路徑的順序,只要在頁面加載的時候設置下Session["FCKeditor:UserFilesPath"]就可以設置FCKeditor上用戶上傳路徑了protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
Session["FCKeditor:UserFilesPath"] = "用戶上傳路徑";
}  (我在配置的時候關閉了文件瀏覽,只提供文件快速上傳)但是在使用的時候如果“Resources/UserUpload/[email protected]”中的[email protected]路徑沒創建,上傳中FCKeditor它不會創建,也導致了文件無法上傳成功,那就需要再修改FCKeditor.net項目中的Uploader.cs文件,添加一段文件夾存在的檢測代碼,如果不存在用戶指定的文件夾側創建一個// Get the uploaded file name.
string sFileName = System.IO.Path.GetFileName( oFile.FileName ) ;
int iCounter = 0 ;
//景裔添加
//檢查上傳目錄是否已經被創建
//開始==========================================
//檢查當前完整路徑是否存在,不存在則開始逐級輪詢檢查,不存則就創建
if (!System.IO.Directory.Exists(UserFilesDirectory))
{
string[] tempDirectorys = UserFilesDirectory.Split(new string[] { "//" }, StringSplitOptions.RemoveEmptyEntries);
string tempDirectory = string.Empty;
for (int i = 0; i {
tempDirectory += tempDirectorys[i] + "//";
if (!System.IO.Directory.Exists(tempDirectory))
System.IO.Directory.CreateDirectory(tempDirectory);
}
}
//結束==========================================
while ( true )
{
string sFilePath = System.IO.Path.Combine( this.UserFilesDirectory, sFileName ) ;
if ( System.IO.File.Exists( sFilePath ) )
{
iCounter++ ;
sFileName =
System.IO.Path.GetFileNameWithoutExtension( oFile.FileName ) +
"(" + iCounter + ")" +
System.IO.Path.GetExtension( oFile.FileName ) ;
iErrorNumber = 201 ;
}
else
{
oFile.SaveAs( sFilePath ) ;
sFileUrl = this.UserFilesPath + sFileName ;
break ;
}
}  這樣就基本解決了多用戶分文件夾上傳圖片的問題,不過也有缺陷的地方,就是當用戶Session超時的時候,用戶再使用瀏覽器上傳文件就不會按照指定用戶文件夾上傳來了,分析這個情況可以得出:這個時候用戶通過編輯器上傳的文件也就是對編輯器內容作出了修改,但是因爲Session超時了,所以可以把做出的修改視作無效,既然修改無效,那用戶上傳的文件也是沒用的,所在我在web.config中又設置了個默認文件上傳位置,所有無效文件都會上傳到這裏,那個回清理的時候也方便多了 不知道哪位大蝦還有更好的辦法。
  [最後編輯於 景裔, at 2007-10-30 09:22:18]
  永久地址:http://blog.breakN.net/feed.asp?q=comment&id=388

本文轉自
http://blog.breakn.net/article.asp?id=388
發佈了332 篇原創文章 · 獲贊 3 · 訪問量 47萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章