Sunday, October 25, 2015

Feed In Iframe form and save file





The code target:
1. embed iframe into a webpage
2. automatic feed in username and password, automatic click login
3. wait 1-3 second, change the dropdown list option from "Toronto" to "Vancouver", click "SaveFile" button
4. automatic save the file to local drive

Current Issue:
Step 1-3 solved, But Step 4 Failed.

Reason: IE8 Security Rule Limitation

-------------------1.  Feedin.html------------------------------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
        <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <meta http-equiv="Pragma" content="no-cache" />
    <meta http-equiv="Cache-Control" content="no-cache" />
    <meta http-equiv="Expires" content="Sat, 01 Dec 2001 00:00:00 GMT" />
    <script src="js/jquery-1.11.3.min.js" type="text/javascript"></script>

    <script src="js/angular.min.js" type="text/javascript"></script>
    <script src="js/angular-sanitize.min.js" type="text/javascript"></script>
    <script src="js/angular.ng-modules.js" type="text/javascript"></script>
    <script src="js/moduleApp.js" type="text/javascript"></script>
    <script src="app/cdse_form.js" type="text/javascript"></script>
    <link rel="stylesheet" type="text/css" href="css/cdse_form.css" />
    <title>Form Test</title>
</head>
<body>
   
<p class="frmClass">Ctrl KC, KU, KD; build: Ctrl+Shift+B;
        form 1</p>
 

<!-- The target: fill the username and password into an Iframe
1. have/find an simple login page, in which a user name and password inputed, once submit, go to another page
2. put this page into iframe
3. on page loading, grab the iframe id, then grab the input box id, -> feed in information
4. click the submit button.
 -->

<iframe width="500" height="100" id="loginIframe" src="login.aspx"></iframe>

    <div id="ng-app" ng-app="cdse_site_app">
    </div>
    <script src="app/loadFormModule.js" type="text/javascript"></script>
<script src="" type="text/javascript">
function startTimerDelay() {
//timer.start();
setTimeout(stopTimer,500);
}

function stopTimer () {
// timer.stop();
var iframeUser = window.document.getElementById('loginIframe').contentWindow.getElementById('txUser');
alert(iframeUser.value);
}
function getInputUser_1()
{

// get the iframe//
var ifrm = document.getElementById('loginIframe');
var doc = ifrm.contentDocument? ifrm.contentDocument: ifrm.contentWindow.document;
// get id
var iframeUser = doc.getElementById('txUser');
/* the final target is to download file, or choose some controls, save files
for this target:
1. choose the common controls, set the common controls
after second, change the control of rpt1.aspx
2. handle download, set popup window value, then save to local folder
*/
var call_ctr_list = doc.getElementById('call_ctr');
var call_ctr_opts_num = call_ctr_list.options.length;


for (var i=0; i<call_ctr_opts_num; i++){
if (call_ctr_list.options[i].value == "MON"){
call_ctr_list.options[i].selected = true;
break;
}
}
// document.getElementById('my_drop_down').selectedIndex=2;
doc.getElementById('call_ctr').selectedIndex=1;
}
function btnSaveExcel_2(){
var ifrm = document.getElementById('loginIframe');
var doc = ifrm.contentDocument? ifrm.contentDocument: ifrm.contentWindow.document;
// get id
var btnSave = doc.getElementById('btnSave');
btnSave.click();
}

$(document).ready(function(){
var ifrm = document.getElementById('loginIframe');
var win = ifrm.contentWindow; // reference to iframe's window
/* The following JavaScript uses these properties to get references to the iframed document's window and document objects: */
// reference to document in iframe
var doc = ifrm.contentDocument? ifrm.contentDocument: ifrm.contentWindow.document;

// wait for a couple of second, then output the message-box
setTimeout(getInputUser_1,1050);
setTimeout(btnSaveExcel_2,3000);

document.getElementById('loginIframe').onload = function() {
if (doc.getElementById('formLogin') != null){
var fmUser = doc.forms['formLogin'].elements['user'];
var fmPass = doc.forms['formLogin'].elements['pass'];
var fmSubmit = doc.forms['formLogin'].elements['submit'];

fmUser.value = 'aaa';
fmPass.value = 'bbb';
fmSubmit.click();
}
else{
}
}
});
</script>

</body>
</html>
-----------------------2. rpt1.aspx---------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="rpt1.aspx.cs" Inherits="ses5.rpt1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script src="js/jquery-1.11.3.min.js" type="text/javascript"></script>
    <script language="javascript" type="text/javascript">
        $(document).ready(function () {
            document.getElementById("btnSave").onclick = function () {
                // enable download a file, and choose ok
                setTimeout("window.location = 'ses5.xls'", 500);
            }
     
        });
    </script>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    Report page
    </div>

    <asp:TextBox ID="txUser" runat="server"></asp:TextBox>
     <asp:TextBox ID="loginStatus" runat="server"></asp:TextBox>

    <asp:DropDownList ID="call_ctr" runat="server">
        <asp:ListItem Value="TOR" Selected="True">TORONTO</asp:ListItem>
        <asp:ListItem Value="VAN">Vancouver</asp:ListItem>
        <asp:ListItem Value="MON">Montreal</asp:ListItem>
    </asp:DropDownList>

    <br />
    <input id="btnSave" type="button" value="SaveFile" /></form>
</body>
</html>
-------------------3. rpt1.aspx.cs ---------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ses5
{
    public partial class rpt1 : System.Web.UI.Page
    {
        System.Web.SessionState.HttpSessionState ses = null;


        protected void Page_Load(object sender, EventArgs e)
        {
         

                string user = Request.QueryString["user"];
                if (user != null)
                {
                    this.txUser.Text = user;
                    // create session and then redirect page
                    Session["username"] = user;
                    this.loginStatus.Text = "login success";
                    // Response.Redirect("~/rpt2.aspx");
                    // base.OnLoad(e);
                }
                else
                {
                    this.txUser.Text = "user not found in queryString";
                    //Response.Redirect("~/login.aspx");
                    // base.OnLoad(e);
                }
         }
    }
}
-----------------------------------------
OO-CSS
SPA’s for mobile devices
JSON - DB
ASPX - DB
MVC
Visual Studio 2015 - Win 7
MVC
Crossbrowser