Gmail - Add Attachments - Reverse Engineering

Discussion in 'Javascript' started by JavaScriptRocks, Aug 3, 2005.

  1. I've been trying to imitate / reverse engineer the add attachment
    feature in gmail composer. I managed to do it to say about 80% but its
    giving me trouble in IE on WinXP-Sp2. I am using PHP to do the upload.
    It works well on Firefox/DeerPark, but in IE, the file selected just
    vanishes. You can verify it by commenting the lines marked "//IE
    Trouble". Commenting those lines will remove IE specific code.

    Please someone help me.

    _____________________________________
    Here is the code
    Tab Size: 8
    -------------------------------------
    <?
    include_once('../modules/file_upload/lib_upload.php');
    ?>
    <script language="JavaScript">

    function Table_AddRow(tblName, pos) {
    if(typeof(pos) == 'undefined')
    pos = '_LAST_';

    var tbl = document.getElementById(tblName);
    switch(pos)
    {
    case '_LAST_':
    pos = tbl.rows.length;
    default:
    pos = parseInt(pos, 10);
    if(isNaN(pos))
    {
    pos = 0;
    }
    else if (pos > tbl.rows.length)
    {
    pos = tbl.rows.length;
    }
    }

    var today = new Date();
    var rowID = tblName + today.getTime() + tbl.rows.length;

    var row = tbl.insertRow(pos);
    if(document.all) row.style.display = 'none'; //IE Trouble
    var cellRight = row.insertCell(row.length);
    cellRight.setAttribute('id', rowID);

    var el = document.createElement('input');
    el.setAttribute('type', 'FILE');
    el.setAttribute('name', 'upload_files[]');
    if(document.all) el.style.display = 'none'; //IE Trouble
    cellRight.appendChild(el);


    if(document.all) //IE Trouble
    { //IE Trouble
    el.click(); //IE Trouble
    if(el.value == '') //IE Trouble
    { //IE Trouble
    DeleteCurRow(row); //IE Trouble
    } //IE Trouble
    else //IE Trouble
    { //IE Trouble
    row.style.display = ''; //IE Trouble
    var fn = document.createElement("strong"); //IE Trouble
    fn.innerHTML = el.value; //IE Trouble
    cellRight.appendChild(fn); //IE Trouble
    } //IE Trouble
    } //IE Trouble
    var spc = document.createElement("span");
    spc.innerHTML = "&nbsp;";
    cellRight.appendChild(spc);

    var aa = document.createElement("a");
    aa.setAttribute('href', 'javascript:;');
    var clickName = new
    Function("Table_DeleteRowByID('"+tblName+"','"+rowID+"')");
    aa.onclick = clickName;
    aa.innerHTML = "remove";
    cellRight.appendChild(aa);
    }

    function Table_DeleteRow(x)
    {
    while (x.tagName.toLowerCase() !='tr')
    {
    if(x.parentElement)
    x=x.parentElement;
    else if(x.parentNode)
    x=x.parentNode;
    else
    return;
    }
    var rowNum=x.rowIndex;
    while (x.tagName.toLowerCase() !='table')
    {
    if(x.parentElement)
    x=x.parentElement;
    else if(x.parentNode)
    x=x.parentNode;
    else
    return;
    }
    x.deleteRow(rowNum);
    }

    function Table_DeleteRowByIndex(tblName, row)
    {
    var tbl = document.getElementById(tblName);
    x.deleteRow(row);
    }

    function Table_DeleteRowByID(tblName, rowID)
    {
    var tbl = document.getElementById(tblName);
    var row = document.getElementById(rowID);
    while (row.tagName.toLowerCase() !='tr')
    {
    if(row.parentElement)
    row = row.parentElement;
    else if(row.parentNode)
    row = row.parentNode;
    else
    return;
    }
    var rowNum = row.rowIndex;
    tbl.deleteRow(rowNum);
    }

    </script>

    <form method="post" enctype="multipart/form-data">
    <table id="tblSample" border="0" cellspacing="2" cellpadding="2">
    <tr id="tblSample_row0">
    <td><a href="javascript:;" onClick="Table_AddRow('tblSample', 0);
    return false;">Add File</a></td>
    </tr></table><input type="hidden" name="MAX_FILE_SIZE"
    value="512000"><input type=submit>
    </form>

    <?php
    if($REQUEST_METHOD == "POST")
    {
    $newNames = array();
    if(($newFile = UploadFile('upload_files', '.', 1, $newNames,
    array('application/x-zip-compressed', 'application/zip'))) === false)
    {
    ?><font color=red><b>Upload of <?= sizeof($APP_ERR)?> file(s)
    failed.</font><br><strong>Error: </strong><? print_r($APP_ERR)?><?
    }
    else
    {
    ?><font color=green><b><?= sizeof($newNames)?> file(s)
    uploaded.<br></font>&nbsp;&nbsp;&nbsp;New filename: <?
    print_r($newNames) ?><?
    }
    }

    ?>

    ________________________________________
    lib_upload.php (skeleton)
    ----------------------------------------
    function UploadFile($name, $path, $mandatory = 0,
    &$newName, $mimeAllowed = array())
    {
    return ($ERR === true? false: $newName);
    }
     
    JavaScriptRocks, Aug 3, 2005
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Li-fan Chen
    Replies:
    0
    Views:
    558
    Li-fan Chen
    Mar 2, 2006
  2. Replies:
    0
    Views:
    877
  3. Replies:
    0
    Views:
    635
  4. codewarrior
    Replies:
    0
    Views:
    539
    codewarrior
    May 4, 2009
  5. JavaScriptRocks

    Gmail - dynamic Add Attachment - Reverse Engineering

    JavaScriptRocks, Aug 4, 2005, in forum: Javascript
    Replies:
    3
    Views:
    193
    Kingstonian
    Aug 7, 2005
Loading...

Share This Page