using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.IO;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// BasePage 的摘要描述
/// </summary>
public class PageBase : System.Web.UI.Page
{
    protected void Alert(string strMsg)
    {
        string strScript = "<language=javascript>\n";
        strMsg += "alert(\"" + strMsg + "\");\n";
        strMsg += "history.back();\n";
        strMsg += "</script>";
        Response.Write(strScript);
    }
    public string UploadFile(string filePath, int maxSize, string[] fileType, System.Web.UI.HtmlControls.HtmlInputFile TargetFile)
    {
        string Result = "UnDefine";
        bool typeFlag = false;
        string FilePath = filePath;
        int MaxSize = maxSize;
        string strFileName, strNewName, strFilePath;
        if (TargetFile.PostedFile.FileName == "")
        {
            return "FILE_ERR";
        }
        strFileName = TargetFile.PostedFile.FileName;
        TargetFile.Accept = "*/*";
        strFilePath = FilePath;
        if (Directory.Exists(strFilePath) == false)
        {
            Directory.CreateDirectory(strFilePath);
        }
        FileInfo myInfo = new FileInfo(strFileName);
        string strOldName = myInfo.Name;
        strNewName = strOldName.Substring(strOldName.Length - 3, 3);
        strNewName = strNewName.ToLower();
        if (TargetFile.PostedFile.ContentLength <= MaxSize)
        {
            for (int i = 0; i <= fileType.GetUpperBound(0); i++)
            {
                if (strNewName.ToLower() == fileType[i].ToString()) { typeFlag = true; break; }
            }
            if (typeFlag)
            {
                string strFileNameTemp = GetUploadFileName();
                string strFilePathTemp = strFilePath;
                strOldName = strFileNameTemp + "." + strNewName;
                strFilePath = strFilePath + "\\" + strOldName;
                TargetFile.PostedFile.SaveAs(strFilePath);
                Result = strOldName;
                TargetFile.Dispose();
            }
            else
            {
                return "TYPE_ERR";
            }
        }
        else
        {
            return "SIZE_ERR";
        }
        return (Result);
    }

    public string UploadFile(string filePath, int maxSize, string[] fileType, string filename, System.Web.UI.HtmlControls.HtmlInputFile TargetFile)
    {
        string Result = "UnDefine";
        bool typeFlag = false;
        string FilePath = filePath;
        int MaxSize = maxSize;
        string strFileName, strNewName, strFilePath;
        if (TargetFile.PostedFile.FileName == "")
        {
            return "FILE_ERR";
        }
        strFileName = TargetFile.PostedFile.FileName;
        TargetFile.Accept = "*/*";
        strFilePath = FilePath;
        if (Directory.Exists(strFilePath) == false)
        {
            Directory.CreateDirectory(strFilePath);
        }
        FileInfo myInfo = new FileInfo(strFileName);
        string strOldName = myInfo.Name;
        strNewName = strOldName.Substring(strOldName.Length - 3, 3);
        strNewName = strNewName.ToLower();
        if (TargetFile.PostedFile.ContentLength <= MaxSize)
        {
            for (int i = 0; i <= fileType.GetUpperBound(0); i++)
            {
                if (strNewName.ToLower() == fileType[i].ToString()) { typeFlag = true; break; }
            }
            if (typeFlag)
            {
                string strFileNameTemp = filename;
                string strFilePathTemp = strFilePath;
                strOldName = strFileNameTemp + "." + strNewName;
                strFilePath = strFilePath + "\\" + strOldName;
                TargetFile.PostedFile.SaveAs(strFilePath);
                Result = strOldName;
                TargetFile.Dispose();
            }
            else
            {
                return "TYPE_ERR";
            }
        }
        else
        {
            return "SIZE_ERR";
        }
        return (Result);
    }

    private string GetUploadFileName()
    {
        string Result = "";
        DateTime time = DateTime.Now;
        Result += time.Year.ToString() + FormatNum(time.Month.ToString(), 2) + FormatNum(time.Day.ToString(), 2) + FormatNum(time.Hour.ToString(), 2) + FormatNum(time.Minute.ToString(), 2) + FormatNum(time.Second.ToString(), 2) + FormatNum(time.Millisecond.ToString(), 3);
        return (Result);
    }

    public string FormatNum(string Num, int Bit)
    {
        int L;
        L = Num.Length;
        for (int i = L; i < Bit; i++)
        {
            Num = "0" + Num;
        }
        return (Num);
    }
}
