2017年12月21日 星期四

Response.End 的Exception -Thread was being aborted

下載檔案發生 exception
Thread was being aborted
本來看到有人說不執行既可
但用Try/catch 包住不執行ThreadAbortException 的例外,一樣會有問題
且違反例外原則

參考了黑大及傑哥的文件後,將 Response.End()拿掉,換成HttpContext.Current.ApplicationInstance.CompleteRequest();
解決;感謝。



 public void DownloadFile( string Files)
    {
        try
        {
            string FileName = Path.GetExtension(Files);
            System.Web.HttpContext.Current.Response.Clear(); //清除buffer
            System.Web.HttpContext.Current.Response.ClearHeaders(); //清除 buffer 表頭
            System.Web.HttpContext.Current.Response.Buffer = false;
            System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
            // 檔案類型還有下列幾種"application/pdf"、"application/vnd.ms-excel"、"text/xml"、"text/HTML"、"image/JPEG"、"image/GIF"
            System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8));
            // 考慮 utf-8 檔名問題,以 out_file 設定另存的檔名
            //System.Web.HttpContext.Current.Response.AppendHeader("Content-Length", attach.Length.ToString()); //表頭加入檔案大小
            //System.Web.HttpContext.Current.Response.BinaryWrite(attach);
            System.Web.HttpContext.Current.Response.WriteFile(Files);
            // 將檔案輸出
            System.Web.HttpContext.Current.Response.Flush();
            // 強制 Flush buffer 內容
            //System.Web.HttpContext.Current.Response.End();
          HttpContext.Current.ApplicationInstance.CompleteRequest();
        }

        catch (ThreadAbortException ex)
        { }
        catch (Exception ex)
        {
            throw ex;
        }

    }

參考:
http://blog.darkthread.net/post-2007-03-08-threadabortexception-when-response-end.aspx
https://dotblogs.com.tw/jeffyang/2017/06/14/214252


2017年12月6日 星期三

SQL 累計加總

SQL 累計加總
原參考 https://www.1keydata.com/tw/sql/sql-running-totals.html

SELECT a1.id, a1.totalqty, SUM(a2.totalqty) Running_Total
FROM eipe.dbo.oqc_bodyspec a1 , eipe.dbo.oqc_bodyspec a2
WHERE a1.totalqty <= a2.totalqty OR (a1.totalqty=a2.totalqty AND a1.id = a2.id)
GROUP BY a1.id, a1.totalqty
ORDER BY a1.totalqty DESC, a1.id DESC;

但計算太久會影響校能,後來在看到 Over 加總後寫出以下的語法

select S.ID,ISNULL(S.totalqty,0),sum(ISNULL(S.totalqty,0)) over(ORDER BY S.ID) SUM_TOTALQTY
from  spec S
ORDER BY S.ID

參考:

撰寫T-SQL來達到累加欄位值的效果,可利用OVER子句並加上SUM函數(SQL Server 2012以上支援)


2017年11月22日 星期三

asp.net -檔案權限控管

因為資料交換的需要,所以用WebAPI 接收資料,但實際上Clinet 只是發送要交換的資料檔案名稱,讓IIS 去讀取xml,待資料交換完畢後,移入Back Folder。


但xml 放置在其他主機,所以IIS 帳號必需要有足夠的權限去管理該資料夾

  • IIS 使用的帳戶為 NETWORK SERVICE 該帳號加入群組權限為 USERS
  • 該Folder開分享資料夾,並設定安全權限-進階
  • USERS-特別權限
    1. 列出資料夾/讀取資料
    2. 建立檔案/寫入資料
    3. 刪除

IIS 執行的身份識別與 Windows 權限控管不是你想的那樣

2017年11月21日 星期二

Crystal Report 固定行數,分頁


設定每頁25行

1.在 Crystal Report 的 rpt 中,產生多個細目區段,並畫入空白行格式(N-1個)。

2.在原來的細目資料中(細目a),開啟「區段專家」,在「在之後新增分頁」點選「公式」,鍵入以下公式 (資料列列表)

if onlastrecord then
false
else
   if RecordNumber mod 25 =0 then
      true
   else
      false

3.每個空白行細目區段中,開啟「區段專家」,在「抑制顯示(無擷取項目)」點選「公式」,鍵入以下公式(補空白列,要幾列,就得新增幾行)


細目b(第一個):

if not onlastrecord or (RecordNumber mod 25 = 0) then
TRUE
else
   if (RecordNumber mod 7 <=1 ) then //第一筆
FALSE
   else
   TRUE

細目c(第二個):

if not onlastrecord or (RecordNumber mod 25 = 0) then
TRUE
else
   if (RecordNumber mod 25 <=2 ) then //第二筆
FALSE
   else
TRUE


來源參考 http://wushinetlife.blogspot.tw/2012/03/crystal-report.html
   



2017年10月18日 星期三

datepicker-日曆週數設定,取一周區間日期

1.週數:發現週數與現行不符,可以自定週數

$(function () {
self.moveTo(0, 0);
self.resizeTo(screen.availWidth, screen.availHeight);
$("#ContentPlaceHolder1_txt_sDt").datepicker({
dateFormat: 'yy-mm-dd',
showButtonPanel: true,
showOn: "both",

buttonImage: "../image/calendar.gif",
buttonImageOnly: true,
buttonText: "日期",
//default:false
showWeek: true,
//顯示周數的標題
//default""wk"
weekHeader: "W",
//自訂計算周數的方法,
//default datepicker.iso8601Week
calculateWeek: myWeekCalc

});
});



function myWeekCalc(date) {
var checkDate = new Date(date.getTime());
checkDate.setDate(checkDate.getDate() + 4 - (checkDate.getDay()));
var time = checkDate.getTime();
//設定1月1日
checkDate.setMonth(0);
checkDate.setDate(1);
return Math.floor(Math.round((time - checkDate) / 86400000) / 7) + 1;
}

2.取得一周的區間

function GetNowWeekPeriod(Curr) {
// 現在日期
var cDt = Curr.replace(/-/g, '/'); //轉換日期格式
var myDate = new Date(cDt);


// 現在是星期幾,日為0、一為1、二為2、三為3、四為4、五為5、六為6
var myDay = myDate.getDay();

// 設成日為7
//if (myDay == 0) {
// myDay = 7;
//}

var myStartDate = new Date(cDt);


// myStartDate.setDate(myStartDate.getDate() + (0 - (myDay - 1)));
//myDate.setDate(myStartDate.getDate() + (0 - (myDay - 1)));
myDate.setDate(myStartDate.getDate() + (0 - (myDay ))); //// 將日期變成目前禮拜的星期一

var myEndDate = new Date(cDt);

// 將日期變成目前禮拜的星期日
myEndDate.setDate(myEndDate.getDate() + (7 - myDay-1));

$("#ContentPlaceHolder1_txt_sDt").val(myDate.format("yyyy/MM/dd")); //輸出格式
$("#ContentPlaceHolder1_txt_eDt").val(myEndDate.format("yyyy/MM/dd"));//輸出格式

}
});

2017年8月9日 星期三

在App_Start的BundleConfig.cs

BundleConfig是VS 2013中的新功能,

    public class BundleConfig
    {

        // For more information on Bundling, visit http://go.microsoft.com/fwlink/?LinkId=254725

        /// <summary>
        /// 在開發時
        /// 設定web.config=> compilation debug="true" 可直接使用.js 檔作開發,
        ///
        /// 在發佈時
        /// 設定web.config=> compilation debug="false" 在發佈於正式網站後,.js .css 系統會自動壓縮,並加密。(*.js=> *.mm.js)
        /// </summary>
        /// <param name="bundles"></param>
        public static void RegisterBundles(BundleCollection bundles)
        {
            #region 放在layout中,所有網頁可共用的js

            //jquery.unobtrusive-ajax.js
            //js-程式庫
            bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                        "~/Scripts/jquery-{version}.js"));
            //js -UI元件
            bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
                        "~/Scripts/jquery-ui-{version}.js"));

            //js -ajax元件 MVC5預設不安裝 ,新增網站需要安裝
            //http://blog.miniasp.com/post/2014/11/10/ASPNET-MVC-5-Microsoft-jQuery-Unobtrusive-Ajax-lost-and-found.aspx
            bundles.Add(new ScriptBundle("~/bundles/jqueryajax").Include(
            "~/Scripts/jquery.unobtrusive-ajax*"));

            //套JavaScript UI程式庫,主要用來在網頁實現MVVM設計模式
            //http://blog.darkthread.net/post-2012-05-09-knockout-js-intro.aspx
            bundles.Add(new ScriptBundle("~/bundles/knockout").Include(
                        "~/Scripts/knockout-{version}.debug.js"));

         

            #endregion
           
            #region 單一網頁include

            //js-表單驗證才需載入
            //Scripts\jquery.validate.unobtrusive.js
            bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                        "~/Scripts/jquery.validate*"));
            #endregion

            #region aps.net 預設/css

            // Use the development version of Modernizr to develop with and learn from. Then, when you're
            // ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
            bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                        "~/Scripts/modernizr-*"));

            bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css"));

            bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(
                        "~/Content/themes/base/jquery.ui.core.css",
                        "~/Content/themes/base/jquery.ui.resizable.css",
                        "~/Content/themes/base/jquery.ui.selectable.css",
                        "~/Content/themes/base/jquery.ui.accordion.css",
                        "~/Content/themes/base/jquery.ui.autocomplete.css",
                        "~/Content/themes/base/jquery.ui.button.css",
                        "~/Content/themes/base/jquery.ui.dialog.css",
                        "~/Content/themes/base/jquery.ui.slider.css",
                        "~/Content/themes/base/jquery.ui.tabs.css",
                        "~/Content/themes/base/jquery.ui.datepicker.css",
                        "~/Content/themes/base/jquery.ui.progressbar.css",
                        "~/Content/themes/base/jquery.ui.theme.css"));
            #endregion



        }
    }

asp.net 呼叫javascript

protected void Btn_Sub_OK_Click(object sender, EventArgs e)
        {

          //call 是個標記,可無;
          //wrong('msg') javascript function ;
          ClientScript.RegisterStartupScript(this.GetType(), "", "wrong('" + show + "')", true);
   

        }