System.Windows.Forms.WebBrowser

在前端寫 JavaScript code 辨別。

    //alert( window.navigator.appName );  // Edge, 11 -> "Netscape"==appName
    engine = null;
    if ( window.navigator.appName == "Microsoft Internet Explorer" || window.navigator.appName == "Netscape" )
    {
       // This is an IE browser. What mode is the engine in?
       if ( document.documentMode ) // IE8 or later
       {
          engine = document.documentMode;
       }
       else // IE 5-7
       {
          engine = 5; // Assume quirks mode unless proven otherwise
          if ( document.compatMode )
          {
             if ( document.compatMode == "CSS1Compat" )
                engine = 7; // standards mode
          }
          // There is no test for IE6 standards mode because that mode  
          // was replaced by IE7 standards mode; there is no emulation.
       }
       // the engine variable now contains the document compatibility mode.
       //alert( engine );  // 10, 9, 8, ...
    }

參考來源:Internet

文章標籤

Robert 發表在 痞客邦 留言(0) 人氣()

包含 <c:out /> 的 "escapeXml" 屬性用途示範。

原始碼:

<%@ taglib prefix="c" uri="/WEB-INF/tlds/c.tld" %>

    <c:if test="${ total >= 2 }" >
        <c:forEach var="i" begin="2" end="${ total }" varStatus="loop">
            <c:out escapeXml="false" value="<p>${i}</p>" />
        </c:forEach>
    </c:if>

注意:迴圈數是<=end屬性數值。

文章標籤

Robert 發表在 痞客邦 留言(0) 人氣()

廢話不多說,
馬上來看原始碼:

設定:

文章標籤

Robert 發表在 痞客邦 留言(0) 人氣()

目前筆記的項目有:「async」、「type」、「contentType」、「success」。
其他的,以後再陸續增加。

$.ajax(
    {
        async: false,  // 非同步:ture(default)/false
        type: "POST",  // request類型:"Get"/"Post"
        dataType: 'html',
        url: 'testPage.do?',
        data: 'serial=online&id=3&title=name',  // url和data結合起來,即是:"testPage? serial=online & id=3 & title=name"
        contentType: "application/x-www-form-urlencoded; charset=ISO-8859-1",  // 'charset'可設定傳輸資料過去的編碼.
        success: function( newHTML ) {  // 如果成功,收到回應資料,要做什麼事。
            document.getElementById( "listId" ).value = newHTML ;
            aUrl = newHTML ;
        }
    }
);  // $.ajax

 

文章標籤

Robert 發表在 痞客邦 留言(0) 人氣()

繼上一篇:[程式][WinForm][C#] backgroundWorker執行緒 如何修改WinForm主執行緒上控制元件的值?

使用 System.Windows.Forms.MethodInvoker 跨執行緒 修改主執行緒的值;

但「public delegate void MethodInvoker();」不能傳參數進去。

文章標籤

Robert 發表在 痞客邦 留言(0) 人氣()

 

程式碼範例:

public class UploadFile
    {
        public UploadFile()
        {
            ContentType = "application/octet-stream";
        }
        public string Name { get; set; }
        public string Filename { get; set; }
        public string ContentType { get; set; }
        public Stream Stream { get; set; }
    }
public byte[] UploadFiles(string address, IEnumerable<UploadFile> files, NameValueCollection values)
    {
        var request = WebRequest.Create(address);
        request.Method = "POST";
        var boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x", NumberFormatInfo.InvariantInfo);
        request.ContentType = "multipart/form-data; boundary=" + boundary;
        boundary = "--" + boundary;

        using (var requestStream = request.GetRequestStream())
        {
            // Write the values
            foreach (string name in values.Keys)
            {
                var buffer = Encoding.ASCII.GetBytes(boundary + Environment.NewLine);
                requestStream.Write(buffer, 0, buffer.Length);
                buffer = Encoding.ASCII.GetBytes(string.Format("Content-Disposition: form-data; name=\"{0}\"{1}{1}", name, Environment.NewLine));
                requestStream.Write(buffer, 0, buffer.Length);
                buffer = Encoding.UTF8.GetBytes(values[name] + Environment.NewLine);
                requestStream.Write(buffer, 0, buffer.Length);
            }

            // Write the files
            foreach (var file in files)
            {
                var buffer = Encoding.ASCII.GetBytes(boundary + Environment.NewLine);
                requestStream.Write(buffer, 0, buffer.Length);
                buffer = Encoding.UTF8.GetBytes(string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"{2}", file.Name, file.Filename, Environment.NewLine));
                requestStream.Write(buffer, 0, buffer.Length);
                buffer = Encoding.ASCII.GetBytes(string.Format("Content-Type: {0}{1}{1}", file.ContentType, Environment.NewLine));
                requestStream.Write(buffer, 0, buffer.Length);
                file.Stream.CopyTo(requestStream);
                buffer = Encoding.ASCII.GetBytes(Environment.NewLine);
                requestStream.Write(buffer, 0, buffer.Length);
            }

            var boundaryBuffer = Encoding.ASCII.GetBytes(boundary + "--");
            requestStream.Write(boundaryBuffer, 0, boundaryBuffer.Length);
        }

        using (var response = request.GetResponse())
        using (var responseStream = response.GetResponseStream())
        using (var stream = new MemoryStream())
        {
            responseStream.CopyTo(stream);
            return stream.ToArray();
        }
    }
using (var stream1 = File.Open("test.txt", FileMode.Open))
    using (var stream2 = File.Open("test.xml", FileMode.Open))
    using (var stream3 = File.Open("test.pdf", FileMode.Open))
    {
        var files = new[] 
        {
            new UploadFile
            {
                Name = "file",
                Filename = "test.txt",
                ContentType = "text/plain",
                Stream = stream1
            },
            new UploadFile
            {
                Name = "file",
                Filename = "test.xml",
                ContentType = "text/xml",
                Stream = stream2
            },
            new UploadFile
            {
                Name = "file",
                Filename = "test.pdf",
                ContentType = "application/pdf",
                Stream = stream3
            }
        };

        var values = new NameValueCollection
        {
            { "key1", "value1" },
            { "key2", "value2" },
            { "key3", "value3" },
        };

        byte[] result = UploadFiles("http://localhost:1234/upload", files, values);
    }

請依所需自行調整。

文章標籤

Robert 發表在 痞客邦 留言(0) 人氣()

一行解決:

string str = System.Text.Encoding.Default.GetString(result);

 

參考:How convert byte array to string

文章標籤

Robert 發表在 痞客邦 留言(0) 人氣()

取得 WebBrowser的cookie:

string cookieStr = webBrowser.Document.Cookie;

存到 CookieContainer:

        private CookieContainer GetCookieContainer( string cookieStr )
        {
            CookieContainer myCookieContainer = new CookieContainer();
            string[] cookstr = cookieStr.Split( ';' );
            foreach ( string str in cookstr )
            {
                string[] cookieNameValue = str.Split( '=' );
                Cookie ck = new Cookie( cookieNameValue[0].Trim().ToString(), cookieNameValue[1].Trim().ToString() );
                ck.Domain = Properties.Settings.Default.ServerDomain ;  // 必須寫對.
                myCookieContainer.Add( ck );
            }

            return myCookieContainer;
        }

使用 HttpWebRequest:

文章標籤

Robert 發表在 痞客邦 留言(0) 人氣()

以 TextBox 為例:

private void bgw1_DoWork(object sender, DoWorkEventArgs e)
{
  {
    this.Invoke(new MethodInvoker(delegate { textBox1.Text = "working.." }));
  }
}

參考:Textbox text from background worker?

 

文章標籤

Robert 發表在 痞客邦 留言(0) 人氣()

專案檔「*.csproj」,開啟編輯其XML檔;
節點:「ProjectExtensions \ VisualStudio \ FlavorProperties \ ProjectProperties」;
改其值:「OfficeVersion="14.0"」

文章標籤

Robert 發表在 痞客邦 留言(0) 人氣()