using system;
using system.data;
using system.data.sqlclient;
using ca.components; //全部在組件名稱空間下
namespace ca.components
{
/// <summary>
/// general 的摘要說明。
/// 發布日期:2002-8-8 原作者:雷神
/// 此程序屬模式小組 http://www.ai361.com/project/
/// 在sql2000,win2000s+.net+iis5中測試通過
/// </summary>
public class general
{
private string filepath; //文件路徑
//定義一個枚舉用來存放文件的信息
public enum file
{
file_size , //大小
file_postname, //
file_sysname ,
file_orginname,
file_path
};
//構造函數
public general()
{
//在web.config中設定appsettings["filepath"],用于存放文件的路徑。
filepath = system.configuration.configurationsettings.appsettings["filepath"];
}
/// <summary>
/// 上傳文件通用函數,此方法為靜態,系統任何時候均可調用。
/// </summary>
/// <param name="file">參數為頁面的file控件對象</param>
/// <returns></returns>
public static string[] uploadfile(htmlinputfile file)
{
string[] arr = new string[5];
//通過系統時間生成文件名,此功能可以封閉掉,不過中文長文件名支持的不好。
string filename = datetime.now.tostring().replace(" ","").replace(":","").replace("-","");
string fileorginname = file.postedfile.filename.substring(file.postedfile.filename.lastindexof("http://")+1);
if(file.postedfile.contentlength<=0)
return null;
string postfilename;
string path = new general().filepath+"http://";
try
{
int pos = file.postedfile.filename.lastindexof(".")+1;
postfilename = file.postedfile.filename.substring(pos,file.postedfile.filename.length-pos);
file.postedfile.saveas(path+filename+"."+postfilename);
}
catch(exception exec)
{
throw(exec);
}
double unit = 1024;
double size = math.round(file.postedfile.contentlength/unit,2);
arr[(int)file.file_size] = size.tostring();//文件大小
arr[(int)file.file_postname] = postfilename;//文件類型(文件后綴名)
arr[(int)file.file_sysname] = filename;//文件系統名
arr[(int)file.file_orginname] = fileorginname;//文件原來的名字
arr[(int)file.file_path]=path+filename+"."+postfilename;//文件路徑
return arr;
//throw(new exception(htmlutility.htmlencode(idno.postedfile.filename)));
}
}
}
if you have any question or comments,please mail to me ([email protected]) or discuss in our forum ( http://www.ai361.com/bbs).
正文完



















