2012年11月29日 星期四

asp 使用三竹簡訊群發的範例


<%@LANGUAGE="VBSCRIPT" CODEPAGE="950"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=big5">
<title>無標題文件</title>
</head>

<body>
<%
'讀取檔案內容
Set fs = CreateObject("Scripting.FileSystemObject")
FileName=server.MapPath("testpost.txt")
Set fw = fs.OpenTextFile(FileName,1,true)
SendBody=fw.ReadAll
fw.close
'正式HTTPS
'sendURL="https://smexpress.mitake.com.tw:9601/SmSendPost.asp?username=XXXXX&password=@@@@@@&encoding=UTF8"
'正式HTTP
'sendURL="http://smexpress.mitake.com.tw:9600/SmSendPost.asp?username=SITTest&password=1234&encoding=UTF8"
'測試HTTP
sendURL="http://163.29.39.31:8000/SmSendPost.asp?username=SITTest&password=1234&encoding=UTF8"
Set objWinHttp = Server.CreateObject("WinHttp.WinHttpRequest.5.1")
objWinHttp.Open "POST", sendURL
objWinHttp.SetRequestHeader "Content-Type","application/x-www-form-urlencoded"
'objWinHTTP.Option(2) = 950 '用來忽略SSL憑證的的錯誤
objWinHTTP.Option(4) = &H3300 '用來忽略SSL憑證的的錯誤
objWinHttp.Send SendBody

'處理從Server端送過來的資料
varBinData=objWinHttp.ResponseBody()
varStrData=Bin2Str(varBinData)

response.write varStrData

'Binary資料轉為String
Function Bin2Str(varBinData)
    Dim varlen,clow,cstrc,skipflag
         skipflag=0
    cstrc = ""
    If Not IsNull(varBinData) Then
      varlen=LenB(varBinData)
      For i=1 To varlen
          If skipflag=0 Then
             clow = MidB(varBinData,i,1)
             '判斷是否中文
             If AscB(clow) > 127 Then
                cstrc =cstrc & Chr(AscW(MidB(varBinData,i+1,1) & clow))
                skipflag=1
             Else
                cstrc = cstrc & Chr(AscB(clow))
             End If
          Else
             skipflag=0
          End If
       Next
    End If
    bin2str = cstrc
End Function
%>
<input name="Submit2" type="button" onClick="javascript:window.opener=null;window.open('','_self');window.close();" value="關閉視窗">
</body>
</html>

asp的同目錄下請放testpost.txt如下


[101]
DestName=經理
dstaddr=0938444585
smbody=我是測試預約15:45發送
dlvtime=20121129154500
response=http://192.168.1.200/smreply.asp
[102]
DestName=二寶
dstaddr=0983444555
dlvtime=20121129155000
smbody=我是測試預約15:50發送
[103]
DestName=小明
dstaddr=0983444114
dlvtime=20121129154000
smbody=我是測試預約15:40發送

2012年11月28日 星期三

c# 使用三竹簡訊群發的範例

http://tw.myblog.yahoo.com/wululu-blog/article?mid=170&next=169&l=f&fid=16

把上面這個例子再簡化說明清楚一點

VS新增一個控制台專案 :


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Net;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
        string tRequestFilePath = @"D:\\a.ini";
        string tUrl = "http://smexpress.mitake.com.tw:9600/SmSendPost.asp?username=XXXXXXXA&password=11111111&encoding=Big5";
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(tUrl);
        request.Timeout = 1000 * 5;
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        //撈出發送資料放到tMSG存成格式為ANSI(必須存成INI格式喔)這裡我直接用一個ini檔來簡化這個例子
        // StreamWriter sw = new StreamWriter(tRequestFilePath, true, System.Text.Encoding.Default);
        // sw.Write(tMSG.ToString());
        // sw.Close();
        //再讀出來
        StreamReader sr = new StreamReader(tRequestFilePath, System.Text.Encoding.Default);
        string strTest = sr.ReadToEnd();
        sr.Close();
        //轉成byte格式
        Byte[] B = System.Text.Encoding.Default.GetBytes(strTest);
        request.ContentLength = B.Length;
        System.IO.Stream ioStream = request.GetRequestStream(); //開始提交數據
        ioStream.Write(B, 0, B.Length);
        ioStream.Close();
        //取得回來訊息
        HttpWebResponse response = request.GetResponse() as HttpWebResponse;
        StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
        string tReturn = reader.ReadToEnd();
        //可以輸出 或存檔tReturn

        Console.WriteLine(tReturn);
        Console.ReadKey();

        reader.Close();        }
    }
}

D:\\a.ini (ansi格式)內容如下


[101]
DestName=經理
dstaddr=0980944485
smbody=我是測試1
response=http://192.168.1.200/smreply.asp
[102]
DestName=二寶
dstaddr=0982944445
smbody=我是測試2
[103]
DestName=小明
dstaddr=0981444444
smbody=我是測試3



2012年11月25日 星期日

ASP表單中關於Enter鍵的控管

處理方式的其中兩種 :
1. 將所有input的Enter鍵模擬成預設按鈕觸發(適用於查詢頁面)

<script>
//menphis 2012.11.26 setting default click button(參考自訊光公司網頁)
$(document).ready(function() {
$("input").bind("keydown", function(event) {
// track enter key
var keycode = (event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode));
if (keycode == 13) { // keycode for enter key
document.getElementById('Mybutton').click();
return false;
} else {
return true;
}
}); // end of function
}); // end of document ready
</script>
2.將input的Enter鍵模擬成Tab鍵(適用於表單內容維護)

所有Input的td中增加 
onkeydown="MyKeyDown();"
並增加以下Function
function 
MyKeyDown(){
if(event.keyCode==13)
event.keyCode=9;
}

2012年10月3日 星期三

Image.FromFile()無法停止檔案使用中的狀態

temprow.Cells(3).Value = Image.FromFile(destFile)
最好是改成這樣
Dim fs As IO.FileStream
fs = New IO.FileStream(destFile, IO.FileMode.Open, IO.FileAccess.Read)

temprow.Cells(3).Value = Image.FromStream(fs).GetThumbnailImage(100, 100, Nothing, IntPtr.Zero)
fs.Close()

才不會出現destFile一直在使用中而無法更新或刪除

*以下程式會出現記憶體不足的問題
fs As IO.FileStream

fs = New IO.FileStream(destFile, IO.FileMode.Open, IO.FileAccess.Read)
tempImage = Image.FromStream(fs)
fs.Close()

temprow.Cells(3).Value = tempImage.GetThumbnailImage(100, 100, Nothing, IntPtr.Zero)

正確寫法如下:

fs As IO.FileStream

fs = New IO.FileStream(destFile, IO.FileMode.Open, IO.FileAccess.Read)
tempImage = Image.FromStream(fs)
temprow.Cells(3).Value = tempImage.GetThumbnailImage(100, 100, Nothing, IntPtr.Zero)
fs.Close()

原因是FileStream要到tempImage的生命週期結束才可以關閉喔!





2012年8月29日 星期三

Sharepoint Foundation Server 上的Office Web Apps無法啟動?

今天架了一個Sharepoint Foundation Server 2010 + SQL 2008 R2 Express with Reporting Service + Office Web Apps 在 不加入網域的獨立Server 2008 R2上, 安裝過程一切順利. 但是Word檔一直無法用網頁編輯. 明明已經啟動Office Web Apps但編輯時一直說我沒有啟動.

突然想起我因為是獨立Server, 所以是用內部IP來設定使用的. 所以馬上改成在hosts中增加一個電腦名對照, 再改用電腦名來設定並使用就一切OK啦!

Sharepoint Foundation Server 2010 + Office Web Apps 的使用心得如下 :
1.線上預覽編輯僅限Word.(免費果然不是好事)
2.我用1顆4核 + 4G Ram的VM來跑, 編輯一個Word時, 開網頁就要將近兩分鐘. 怪不得微軟建議要雙4核CPU + 16G的RAM來供10個人同時編輯. 真是個坑爹的貨啊.

2012年5月31日 星期四

第一次部署ASP.NET網站(耍笨全記錄)

1.Windows 2003 R2 + ASP.NET 4.0
有問題請參考保哥的說明來做喔
http://blog.miniasp.com/post/2010/06/IIS-6-ASPNET-4-Installation-Notes.aspx

2.Crystal report runtime無法正常執行嗎?
請參考(重點是rpt檔及安裝目錄)
http://www.langyuweb.com/a/wangluokaifa/2011/0901/4984.html

3.發佈時要注意所有DLL及Image是否有打包進去
http://www.dotblogs.com.tw/neil/archive/2012/02/11/68874.aspx

經過一番耍笨的動作..還是成功的將Web Form部署到Win2003 R2及Win2008兩個Server上了..

不過單鍵發行還是做不起來...:繼續努力吧!



2012年5月22日 星期二

Webform's GridView使用DropDownList時, 如何不用隱藏欄位來傳遞原值呢?

ASP的程式片段


                       <asp:TemplateField HeaderText="業務組">
                            <ItemTemplate>
                                <asp:Label ID="Lab_Department" runat="server" Text='<%# Eval("Department") %>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:DropDownList ID="ddl_Department2" runat="server" Width="10em"></asp:DropDownList>
                            </EditItemTemplate>
                            <ItemStyle HorizontalAlign="Left" Wrap="False" />
                        </asp:TemplateField>

asp.cs的程式片段

1.先定義Class變數
string Group;


2.在GridView CaseLists的RowEditing的事件中把ItemTemplate中的原值存到Group.
        protected void CaseLists_RowEditing(object sender, GridViewEditEventArgs e)
        {
            CaseLists.EditIndex = e.NewEditIndex;
            Group = ((Label)CaseLists.Rows[e.NewEditIndex].FindControl("Lab_Department")).Text.Trim();
            BindOrder2();
        }

3.將SelectedValue設為Group即可
        protected void CaseLists_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)//判定当前的行是否属于datarow类型的行
            {
                if ((e.Row.RowState & DataControlRowState.Edit) != 0)
                {
                    if (e.Row.FindControl("ddl_Department2") != null)
                    { 

                        //綁定業務組下拉選單
                        ((DropDownList)e.Row.FindControl("ddl_Department2")).DataSource = CreateDataTable();
                        ((DropDownList)e.Row.FindControl("ddl_Department2")).DataTextField = "Items";
                        ((DropDownList)e.Row.FindControl("ddl_Department2")).DataValueField = "Items";
                        ((DropDownList)e.Row.FindControl("ddl_Department2")).SelectedValue = Group;
                        ((DropDownList)e.Row.FindControl("ddl_Department2")).DataBind();                   
                    }
                }
....}}