如果你的站比較大,那么可能就要修改上傳文件的方法,加上自動重命名和分月目錄,修改Uploader.cs:
/*
* FCKeditor - The text editor for internet
* Copyright (C) 2003-2005 Frederico Caldeira Knabben
*
* Licensed under the terms of the GNU Lesser General Public License:
* http://www.opensource.org/licenses/lgpl-license.php
*
* For further information visit:
* http://www.fckeditor.net/
*
* "Support Open Source software. What about a donation today?"
*
* File Name: Uploader.cs
* This is the code behind of the uploader.aspx page used for Quick Uploads.
*
* File Authors:
* Frederico Caldeira Knabben (fredck@fckeditor.net)
*/
using System ;
using System.Globalization ;
using System.Xml ;
using System.Web ;
namespace FredCK.FCKeditorV2
{
public class Uploader : FileWorkerBase
{
protected override void OnLoad(EventArgs e)
{
// Get the posted file.
HttpPostedFile oFile = Request.Files["NewFile"];
// Check if the file has been correctly uploaded
if (oFile == null || oFile.ContentLength == 0)
{
SendResults(202);
return;
}
int iErrorNumber = 0;
string sFileUrl = "";
// Get the uploaded file name.
//string sFileName = System.IO.Path.GetFileName( oFile.FileName ) ;
//檢查建立分月目錄
string sFolder = DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString();
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(System.IO.Path.Combine(this.UserFilesDirectory, sFolder));
if (!dir.Exists)
{
dir.Create();
}
//根據(jù)日期和隨機數(shù)設(shè)置自動重命名文件
Random rd = new Random();
string sFileName = sFolder + "/" + DateTime.Now.ToString("yyyyMMddHHmmss") + rd.Next(10).ToString();
int iCounter = 0;
while (true)
{
//防止沒有擴展名的處理,棄用內(nèi)置方法
string sFileExtension = string.Empty;
try
{
sFileExtension = oFile.FileName.Substring(oFile.FileName.LastIndexOf('.'), oFile.FileName.Length - oFile.FileName.LastIndexOf('.'));
}
catch
{
//Exception ex = new Exception(oFile.FileName);
//throw ex;
sFileExtension = ".jpg";
}
string sFilePath = System.IO.Path.Combine(this.UserFilesDirectory, sFileName) + sFileExtension;
if (System.IO.File.Exists(sFilePath))
{
iCounter++;
sFileName =
sFileName +
"(" + iCounter + ")" +
sFileExtension;
iErrorNumber = 201;
}
else
{
oFile.SaveAs(sFilePath);
sFileUrl = System.IO.Path.Combine(this.UserFilesPath, sFileName + sFileExtension);
break;
}
}
SendResults(iErrorNumber, sFileUrl, sFileName);
}
#region SendResults Method
private void SendResults( int errorNumber )
{
SendResults( errorNumber, "", "", "" ) ;
}
private void SendResults( int errorNumber, string fileUrl, string fileName )
{
SendResults( errorNumber, fileUrl, fileName, "" ) ;
}
private void SendResults( int errorNumber, string fileUrl, string fileName, string customMsg )
{
Response.Clear() ;
Response.Write( "<script type="text/javascript">" ) ;
Response.Write( "window.parent.OnUploadCompleted(" + errorNumber + ",'" + fileUrl.Replace( "'", "\'" ) + "','" + fileName.Replace( "'", "\'" ) + "','" + customMsg.Replace( "'", "\'" ) + "') ;" ) ;
Response.Write( "</script>" ) ;
Response.End() ;
}
#endregion
}
}
* FCKeditor - The text editor for internet
* Copyright (C) 2003-2005 Frederico Caldeira Knabben
*
* Licensed under the terms of the GNU Lesser General Public License:
* http://www.opensource.org/licenses/lgpl-license.php
*
* For further information visit:
* http://www.fckeditor.net/
*
* "Support Open Source software. What about a donation today?"
*
* File Name: Uploader.cs
* This is the code behind of the uploader.aspx page used for Quick Uploads.
*
* File Authors:
* Frederico Caldeira Knabben (fredck@fckeditor.net)
*/
using System ;
using System.Globalization ;
using System.Xml ;
using System.Web ;
namespace FredCK.FCKeditorV2
{
public class Uploader : FileWorkerBase
{
protected override void OnLoad(EventArgs e)
{
// Get the posted file.
HttpPostedFile oFile = Request.Files["NewFile"];
// Check if the file has been correctly uploaded
if (oFile == null || oFile.ContentLength == 0)
{
SendResults(202);
return;
}
int iErrorNumber = 0;
string sFileUrl = "";
// Get the uploaded file name.
//string sFileName = System.IO.Path.GetFileName( oFile.FileName ) ;
//檢查建立分月目錄
string sFolder = DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString();
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(System.IO.Path.Combine(this.UserFilesDirectory, sFolder));
if (!dir.Exists)
{
dir.Create();
}
//根據(jù)日期和隨機數(shù)設(shè)置自動重命名文件
Random rd = new Random();
string sFileName = sFolder + "/" + DateTime.Now.ToString("yyyyMMddHHmmss") + rd.Next(10).ToString();
int iCounter = 0;
while (true)
{
//防止沒有擴展名的處理,棄用內(nèi)置方法
string sFileExtension = string.Empty;
try
{
sFileExtension = oFile.FileName.Substring(oFile.FileName.LastIndexOf('.'), oFile.FileName.Length - oFile.FileName.LastIndexOf('.'));
}
catch
{
//Exception ex = new Exception(oFile.FileName);
//throw ex;
sFileExtension = ".jpg";
}
string sFilePath = System.IO.Path.Combine(this.UserFilesDirectory, sFileName) + sFileExtension;
if (System.IO.File.Exists(sFilePath))
{
iCounter++;
sFileName =
sFileName +
"(" + iCounter + ")" +
sFileExtension;
iErrorNumber = 201;
}
else
{
oFile.SaveAs(sFilePath);
sFileUrl = System.IO.Path.Combine(this.UserFilesPath, sFileName + sFileExtension);
break;
}
}
SendResults(iErrorNumber, sFileUrl, sFileName);
}
#region SendResults Method
private void SendResults( int errorNumber )
{
SendResults( errorNumber, "", "", "" ) ;
}
private void SendResults( int errorNumber, string fileUrl, string fileName )
{
SendResults( errorNumber, fileUrl, fileName, "" ) ;
}
private void SendResults( int errorNumber, string fileUrl, string fileName, string customMsg )
{
Response.Clear() ;
Response.Write( "<script type="text/javascript">" ) ;
Response.Write( "window.parent.OnUploadCompleted(" + errorNumber + ",'" + fileUrl.Replace( "'", "\'" ) + "','" + fileName.Replace( "'", "\'" ) + "','" + customMsg.Replace( "'", "\'" ) + "') ;" ) ;
Response.Write( "</script>" ) ;
Response.End() ;
}
#endregion
}
}
原文地址:http://blog.csdn.net/lonestone/archive/2006/08/14/1062870.aspx