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 System.IO;
using Spaces;
using Xiaoxiao.Upload;

public partial class Admin_AlbumPost : System.Web.UI.Page
{
    bool CheckExt(string ext)
    {
        bool chk = false;
        switch (ext.ToLower())
        {
            case ".jpg":
                chk = true;
                break;
            case ".jpeg":
                chk = true;
                break;
            case ".gif":
                chk = true;
                break;
            case ".bmp":
                chk = true;
                break;
            case ".tif":
                chk = true;
                break;
            case ".png":
                chk = true;
                break;
        }
        return chk;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        
        string key = Request.Form["fileKey"];

        //fileContent
        //
        UploadFile uploadFile = UploadHelper.GetUploadFile("fileContent");
       // uploadFile.AllowExt = "*";
        // Response.Write(Request.Form);
       // Response.End();
        //HttpPostedFile pf = Request.Files["fileContent"];
        if (User.Identity.Name==string.Empty ||uploadFile == null)
        {
            string msg = "<script type='text/javascript'>parent.report_upload_error('很抱歉，上传图片时发生错误　！：）', '');</script>";
            Response.Write(msg);
            Response.End();
        }
        long fileLength = uploadFile.ContentLength;
        if (fileLength > (1024 * 1024) * 1)
        {
            string msg = "<script type='text/javascript'>parent.report_upload_error('很抱歉，上传图片时发生错误，图片文件太大了，请压缩后在上传　！：）', '');</script>";
            Response.Write(msg);
            Response.End();
        }

       // byte[] myData = new Byte[fileLength];
       // pf.InputStream.Read(myData, 0, pf.ContentLength);

        string sFilename = System.IO.Path.GetFileName(uploadFile.FileName);
        string ext = Path.GetExtension(uploadFile.FileName);
        if (!CheckExt(ext))
        {
            return;
        }
        int file_append = 0;
        string path = Path.Combine(Config.GetConfig().BlogAlbumPath, Account);
        if (!path.EndsWith("\\"))
            path += "\\";
        string cPath = string.Format("{0}_{1}_{2}",  DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
        path = string.Format("{0}{1}\\",path,cPath);
        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }
        //如果文件名存在，在文件名后加一
        while (System.IO.File.Exists(path + sFilename))
        {
            file_append++;
            sFilename = System.IO.Path.GetFileNameWithoutExtension(uploadFile.FileName)
                + file_append.ToString() + System.IO.Path.GetExtension(uploadFile.FileName);
        }

     
       // System.IO.FileStream newFile = new System.IO.FileStream(path + sFilename, System.IO.FileMode.Create);
       // newFile.Write(myData, 0, myData.Length);
       // newFile.Close();
        uploadFile.SaveAs(path + sFilename);

        string scr = string.Format("   <script>parent.report_upload_ok('{0}','{1}');</script>", key, string.Format("{0}/{1}/{2}",  Account , cPath , sFilename));
        Response.Write(scr);
    }
    string Account
    {
        get { return SpacesContext.Current.Context.User.Identity.Name; }
    }
}
