TableToExcel jQuery causes a strange error in IE

I am using jQuery function to export my HTML table to Excel. This is a feature I've seen in many other places, and it works great for me in Chrome:

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)) } })() 

However, in IE 10 this line: "window.location.href = uri + base64 (format (template, ctx))" gives an error: "SCRIPT122: the data area passed to the system call is also small."

I did a bit of work, and for some reason, IE cannot handle the length of the URI. Are there any workarounds?

+4
source share
1 answer

You, my friend, are in great trouble (just kidding)!

The main problem with older versions of IE is support for base64 encoding, which you can take care of using this Javascript library .

But the main problem is the data URL scheme, which is available in WebKit-based browsers, and you can use it in RCA-2397-based IE. It has limited functionality and mainly works with images and css. Here is a useful link. Also be sure to read the Wikipedia page Data URL Schema

** Now let's talk about workarounds! **

The main solution is a server side write script that creates an Excel file, and you just use an Ajax call to send it back to the user! You can even save this file on your server. File system and just send the address of this file.

Although, if you mainly focus on the client side, and you really need to work with IE, you should use the download library, preferably those that have swf (Flash support), that's what I haven't used yet, although I hope to get a result that you are looking for. There are allegations that downloadify does this well and let me know how this happens!

+4
source

Source: https://habr.com/ru/post/1500584/


All Articles