Beispiel #1
0
    public string uploadFiles(HttpPostedFile upload_files)
    {
        string  response  = "";
        int     sleeptime = 5000;
        string  files_id  = "";
        double  file_size = (double)upload_files.InputStream.Length / 1000000;
        utility utlObj    = new utility();

        // ======== Log ========
        UserLoginData login_data = (UserLoginData)HttpContext.Current.Session["login_data"];
        LogData       log_data   = new LogData();

        log_data.ModuleCode = new LogData().Module_News;
        log_data.Username   = login_data.User_ID;
        // =====================

        if (file_size > 0)
        {
            files_id = utlObj.getNewsID("News_Files", "FILES_ID");

            for (int i = 0; i < 10; i++)
            {
                if (getFiles(files_id).Files_ID != null)
                {
                    System.Threading.Thread.Sleep(sleeptime);
                    files_id   = utlObj.getNewsID("News_Files", "FILES_ID");
                    sleeptime += 5000;
                }
                else
                {
                    break;
                }
            }

            string Extension = Path.GetExtension(upload_files.FileName);
            string FileName  = files_id + Extension;

            string FolderPath = ConfigurationManager.AppSettings["NewsUploadFiles"];

            string FilePath = HttpContext.Current.Server.MapPath(FolderPath + (FileName));

            // ======== Log ========
            log_data.Message = "Upload Files with:" + FileName + "," + file_size;
            // =====================

            try
            {
                upload_files.SaveAs(FilePath);
                // === Log ===
                log_data.StatusCode = log_data.LogStatus_SUCCESS;
                // ===========
            }
            catch (Exception e)
            {
                // === Log ===
                log_data.StatusCode = log_data.LogStatus_ERROR;
                log_data.Message   += " [" + e.ToString() + "]";
                // ===========
            }


            NewsFilesData files_data = new NewsFilesData();
            files_data.Files_ID   = files_id;
            files_data.Files_Name = FileName;
            files_data.Files_Ext  = Extension;
            files_data.Files_Size = file_size;



            if (insertFilesData(files_data) == "OK")
            {
                response = files_id;
            }
        }
        else
        {
            // === Log ===
            log_data.StatusCode = log_data.LogStatus_FAILED;
            log_data.Message   += " [Size is Zero]";
            // ===========
        }

        new Log().WriteLog(log_data);

        return(response);
    }
Beispiel #2
0
    public string insertNews(NewsData dataInsert, FileUpload upload_files)
    {
        string        response    = "";
        List <string> AllFiles_ID = new List <string>();

        utility         utlObj     = new utility();
        Status_Variable status_var = new Status_Variable();

        if (upload_files.HasFiles)
        {
            foreach (HttpPostedFile postedFile in upload_files.PostedFiles)
            {
                string files_id = "";


                files_id = uploadFiles(postedFile);

                if (files_id != "")
                {
                    AllFiles_ID.Add(files_id);
                }
            }
        }
        else
        {
            AllFiles_ID.Add("-");
        }


        if (AllFiles_ID.Count > 0)
        {
            /*  เพิ่มข้อมูลข่าว */

            string        news_table = "News";
            string        news_field = "News_ID";
            ConnectDB     db         = new ConnectDB();
            SqlDataSource oracleObj  = db.ConnectionOracle();

            string sql = "";

            int sleeptime = 5000;

            string news_id = utlObj.getNewsID(news_table, news_field);

            for (int i = 0; i < 10; i++)
            {
                if (getNews(news_id).news_id != null)
                {
                    System.Threading.Thread.Sleep(sleeptime);
                    news_id    = utlObj.getNewsID(news_table, news_field);
                    sleeptime += 5000;
                }
                else
                {
                    break;
                }
            }

            UserLoginData login_data = (UserLoginData)HttpContext.Current.Session["login_data"];

            sql = "Insert Into NEWS(NEWS_ID,NEWS_TOPIC,NEWS_DETAIL,NEWS_TYPE,NEWS_DATETIME,NEWS_POSTBY,NEWS_READ) Values('" + news_id + "','" + dataInsert.news_topic + "','" + dataInsert.news_detail + "','" + dataInsert.news_type + "','" + DateTime.Now.ToString() + "','" + login_data.User_ID + "'," + 0 + ")";

            oracleObj.InsertCommand = sql;

            // ======== Log ========
            LogData log_data = new LogData();
            log_data.ModuleCode = new LogData().Module_News;
            log_data.Username   = login_data.User_ID;
            log_data.Message    = "Add News with:" + news_id + "," + dataInsert.news_type;
            // =====================

            try
            {
                if (oracleObj.Insert() == 1)
                {
                    response = "OK";
                    // === Log ===
                    log_data.StatusCode = log_data.LogStatus_SUCCESS;
                    // ===========
                }
                else
                {
                    // === Log ===
                    log_data.StatusCode = log_data.LogStatus_FAILED;
                    // ===========
                }
            }
            catch (Exception e)
            {
                // === Log ===
                log_data.StatusCode = log_data.LogStatus_ERROR;
                log_data.Message   += " [" + e.ToString() + "]";
                // ===========
                response = e.Message.ToString();
                HttpContext.Current.Session["response"] = "Unit Test:News:insertNews" + " ไม่สามารถดำเนินการได้";
                HttpContext.Current.Response.Redirect("err_response.aspx");
            }

            /* ========= */
            new Log().WriteLog(log_data);

            if (response == "OK" && AllFiles_ID.Count > 0)
            {
                foreach (string files_id in AllFiles_ID)
                {
                    if (files_id != "-")
                    {
                        setNewsID_to_FilesData(files_id, news_id);
                    }
                }
            }
        }


        return(response);
    }