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("\\")+1);
if(file.PostedFile.ContentLength<=0)
return null;
string postFileName;
string path = new general().FilePath+"\\";
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)));
}
}
}