using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using Enties;

using OAFrameWork;

using BusinessLogical;

using System.Text.RegularExpressions;

using System.Text;

using System.IO;

public partial class ascx_HtmlEdit : System.Web.UI.UserControl

{

    public string Inputbh;

    public string NewInputPath;//输入文件路径  

    public string InputTxtbt;      

    private string FilePath;

    private TextFile text;

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack)

        {

            FilePath = "../"+Txtfilelink.Text;


            text = new TextFile(Server.MapPath(FilePath));

            if (FilePath != "")              

                webupload.Value = text.readFile();

        }

    }

    public string utfEncode(string temp)

    {

        //string gb2312info = temp;

        //string utfinfo = string.Empty;

        //Encoding utf8 = Encoding.UTF8;

        //Encoding gb2312 = Encoding.GetEncoding("unicode");

        Convert the string into a byte[].

        //byte[] unicodeBytes = gb2312.GetBytes(gb2312info);

        Perform the conversion from one encoding to the other.

        //byte[] asciiBytes = Encoding.Convert(utf8, gb2312, unicodeBytes);

        Convert the new byte[] into a char[] and then into a string.

        This is a slightly different approach to converting to illustrate

        the use of GetCharCount/GetChars.

        //char[] asciiChars = new char[utf8.GetCharCount(asciiBytes, 0, asciiBytes.Length)];

        //utf8.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);

        utfinfo = new string(asciiChars);

        //utfinfo = System.Text.Encoding.UTF8.GetString(asciiBytes);

        //return utfinfo;

        string strAnsi = temp;

        byte[] bytDatas = System.Text.Encoding.Default.GetBytes(strAnsi);

        string strUTF8 = System.Text.Encoding.GetEncoding("utf-8").GetString(bytDatas);

        return strUTF8;

    }

    protected void Conferm_Click(object sender, EventArgs e)

    {

            FilePath = "../" + Txtfilelink.Text;

            File.Delete(Server.MapPath(FilePath));//删除原文件

            FilePath = "../UpLoad/gg/" + Txtbt.Text + ".htm";

            text = new TextFile(Server.MapPath(FilePath));

            text.writeFile(webupload.Value.ToString().Replace("../../upload/gg/images/","images/"));//写入新文件

            //Conferm.PostBackUrl = this.Request.Url.ToString();

            Inputbh = Txtbh.Text;//文件编号

            InputTxtbt = Txtbt.Text;

            NewInputPath = "UpLoad/gg/" + InputTxtbt + ".htm";           

            string updatesql = "update File_detail set bt='" + InputTxtbt + "',filelink='" + NewInputPath + "'where bh='" + Inputbh + "'";

            try

            {

                ActiveCon.DataFactory.ExecuteCommand(updatesql);

            }

            catch (Exception ex)

            {

                throw ex;

            }                

            Response.Write("<script language=javascript>window.close();</script>");


    }


}


public class TextFile

{

    private string FILE_NAME;

    private string fileContent;

    private System.Text.Encoding enCode;

    public TextFile(String fileName)

    {

        FILE_NAME = fileName;

        enCode = System.Text.Encoding.Default;

    }

    public String readFile()//读取web文件到FCK

    {

        if (!File.Exists(FILE_NAME))

        {

            Console.WriteLine("{0} does not exist.", FILE_NAME);

           // CMsgBox.Show ("文件不存在");

            return null;

        }

        using (StreamReader sr = new StreamReader(FILE_NAME, enCode))

        {

            string input;

            while ((input = sr.ReadLine()) != null)

            {

                //input.Replace("gb2312", "utf-8");

                Console.WriteLine(input);

                fileContent += input;

            }        

            Console.WriteLine("The end of the stream has been reached.");

            sr.Close();

        }     

        return fileContent;

    }

    public void writeFile(string strContent)//写入文件

    {

        fileContent=strContent;

        if (!File.Exists(FILE_NAME))

        {

            Console.WriteLine("{0} does not exist.", FILE_NAME);

        }

        StreamWriter sw = new StreamWriter(FILE_NAME, false, enCode);

        {

            //String input;

            //while ((input = sw.ReadLine()) != null)

            //{

            //    //input.Replace("gb2312", "utf-8");

            //    Console.WriteLine(input);

            //    fileContent += input;

            //}

            //Console.WriteLine("The end of the stream has been reached.");

            sw.Write(fileContent);

            sw.Close();

        }

    }

}