0

The following code can generate a xls file. My problem is how the send the xls file to a email box as an attachment.

<script type="text/javascript">
function fillHidTable(){
    var htqf; //-- hidden field
    var rf; //-- retrieved field
    for ( var i = 1; i < 6; i++ ) {
        rf = "htqf"+i;
        document.getElementById(rf).innerHTML = document.getElementById("Q"+i+"CALC").value;
    }
    tableToExcel('hidTable', 'Analysis Results');
}

var tableToExcel = (function() {
    var uri = 'data:application/vnd.ms-excel;base64,'
            , template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>'
            , base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }
            , format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) }
    return function(table, name) {
        if (!table.nodeType) table = document.getElementById(table)
        var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
        window.location.href = uri + base64(format(template, ctx))
    
})()
</script>
ZygD
  • 22,092
  • 39
  • 79
  • 102
  • afaik, you can't add attachments to a mailto: link, only to, cc, from, subject and body – dandavis Jun 30 '15 at 02:40
  • It's not currently possible just with client-side JavaScript. See [Sending emails with Javascript](http://stackoverflow.com/questions/271171/sending-emails-with-javascript) and [using mailto to send email with an attachment, html or jsf](http://stackoverflow.com/questions/5233556/using-mailto-to-send-email-with-an-attachment-html-or-jsf). – Jonathan Lonowski Jun 30 '15 at 02:43

1 Answers1

0

Not with JavaScript alone but u can try using any other programming language like java . Use a ajax request with your attachment as a post request

chandan_kr_jha
  • 557
  • 4
  • 12