When I try to download HTML table ( it contains 2600 rows ) as an excel file, the browser going to crash.However when it contains less records(rows) it's working fine.
For Downloading table I created a link and clicking it virtually. Here is the code.
var base64data = "base64," + $.base64.encode(excelFile);
var result = 'data:' + 'application/vnd.ms-' + defaults.type + ';charset=UTF-8;filename=exportData.doc;' + base64data;
var link = document.createElement('a');
link.download = defaults.fileName + '.xls';
link.href = result;
link.click();
Is there any other way to download with out crashing the browser?
Related
I am working in Alfresco and trying to download the file content as CSV file.
The download works, but only problem is that when I download a content that has dash or em dash inside, when I open that CSV file I see %u2013 and %u2014 instead.
This is my code for that part, I use JavaScript, but I do know how to solve this issue:
var blob = new Blob([CSV], {type: "text/csv;charset=utf-8;"});
if (window.navigator.msSaveOrOpenBlob && window.Blob) {
navigator.msSaveOrOpenBlob(blob, fileName + ".csv");
} else {
var uri = 'data:text/csv;charset=utf-8,' + escape(CSV);
var link = document.createElement("a");
link.href = uri;
link.style = "visibility:hidden";
link.download = fileName + ".csv";
document.body.appendChild(link);
if (navigator.userAgent.search("Chrome") > 0) {
setTimeout(function () {
link.click();
}, 2000);
} else {
link.click();
}
}
document.body.removeChild(link);
For example, value named abolix-fix after the download inside CSV file it is like this abolix%u2013fix.
Thanks in advance!
I have js function which exporting HTML page to doc file. The function works great, but in the exported doc file extra line gap is occurring as compare to the original html page and it's not A4 size page. I need to make the doc file page size A4 and remove the extra line gap. Can somebody help me?
I have researched about exporting HTML to doc/docx and tried some js code it is converting well but css page size code is not working.
//Here is my html code:
<div id="doc_block">
// in this div content appends
<div id="view_delivered_articles_modal_quill_editor" class="WordSection1" style="display: none;"></div>
</div>
// here is the js code:
$(document).on('click', '.download_link_quill_editor', function(){
Export2Doc();
});
function Export2Doc(filename = ''){
var preHtml = "<html xmlns:o='urn:schemas-microsoft-
com:office:office' xmlns:w='urn:schemas-microsoft-
com:office:word' xmlns='http://www.w3.org/TR/REC-
html40'><head><meta charset='utf-8'><title>Export HTML
To Doc</title></head><body>";
var postHtml = "</body></html>";
if (!window.Blob) {
alert('Your legacy browser does not support this action.');
return;
}
var html, link, blob, url, css;
// EU A4 use: size: 841.95pt 595.35pt;
// US Letter use: size:11.0in 8.5in;
css = (
'<style>' +
'#page WordSection1{size: 841.95pt 595.35pt;mso-page-
orientation: portrait;}' +
'div.WordSection1 {page: WordSection1;}' +
'</style>'
);
html = preHtml + window.doc_block.innerHTML + postHtml;
blob = new Blob(['\ufeff', css + html], {
type: 'application/msword'
});
url = URL.createObjectURL(blob);
link = document.createElement('A');
link.href = url;
// Set default file name.
// Word will append file extension - do not add an extension here.
link.download = 'Document';
document.body.appendChild(link);
if (navigator.msSaveOrOpenBlob )
navigator.msSaveOrOpenBlob( blob, 'Document.doc'); // IE10-11
else link.click(); // other browsers
document.body.removeChild(link);
}
// I expect the export doc file size should be A4 and extra line gap to be removed.
I have created a Shiny app that outputs data in a data table. As the data table contains dropdown menus that are injected, the default download button of Shiny and datatable do not work (either outputs all, including all dropdown menu options, or outputs HTML). I am trying to use Javascript to export the data that I have. For that I am using the following external Javascript function downloadcsv.js that I have created based on the following guide:
shinyjs.myfunction = function(args) {
var data, filename, link, result, ctr, keys, columnDelimiter, lineDelimiter;
columnDelimiter = ',';
lineDelimiter = '\n';
keys = Object.keys(data[0]);
result = '';
result += keys.join(columnDelimiter);
result += lineDelimiter;
data.forEach(function(item) {
ctr = 0;
keys.forEach(function(key) {
if (ctr > 0) result += columnDelimiter;
result += item[key];
ctr++;
});
result += lineDelimiter;
});
csv = result;
filename = 'export.csv';
if (!csv.match(/^data:text\/csv/i)) {
csv = 'data:text/csv;charset=utf-8,' + csv;
}
data = encodeURI(csv);
link = document.createElement('a');
link.setAttribute('href', data);
link.setAttribute('download', filename);
link.click();
}
In ui.R of my Shiny app I have added a download button using:
shinyjs::useShinyjs(),
extendShinyjs(script = "downloadcsv.js"),
actionButton("download", "Download"),
In server.R I have added:
onclick("download", js$myfunction(data))
However, when clicking the Download button, nothing happens. Running Shiny in a browser or in a window in RStudio does not make a difference. It seems that the link.click(); line is not executed, or that the download link is not created at all. The output in the console is as follows when running the app with shiny.trace = TRUE.
SEND {"busy":"busy"}
SEND {"custom":{"shinyjs-myfunction":{"data":["test1","test2"]}}}
SEND {"busy":"idle"}
So after clicking the button, the Javascript function seems to be executing, but no output/download appears.
So indeed the issue was in the last 4 lines of code link. I have replaced the four lines with similar code. But added a required line for use in FF:
var encodedUri = encodeURI(csv);
var link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", "my_data.csv");
document.body.appendChild(link); // Required for FF
link.click(); // This will download the data file named "my_data.csv".
Based on How to export JavaScript array info to csv (on client side)?
I only have to find a way to correctly pass the data from R to the Javascript function.
edit: I have found how to do that here!
var link = document.createElement("a");
link.id="lnkDwnldLnk";
//this part will append the anchor tag and remove it after automatic click
document.body.appendChild(link);
var csv = CSV;
blob = new Blob([csv], { type: 'text/csv' });
var csvUrl = window.webkitURL.createObjectURL(blob);
var filename = "CCUDetail_";
$("#lnkDwnldLnk")
.attr({
'download': filename,
'href': csvUrl
});
$('#lnkDwnldLnk')[0].click();
document.body.removeChild(link);
Here it is showing script error that blob is undefined in ie 11.
Use this for IE,
var IEwindow = window.open();
IEwindow.document.write('sep=,\r\n' + CSV);
IEwindow.document.close();
IEwindow.document.execCommand('SaveAs', true, fileName + ".csv");
IEwindow.close();
For more information i have written tutorial on that,
see - Download JSON data in CSV format Cross Browser Support
Hope this will be helpful for you.
I wrote this export button that basically spits out all the data I have on the google table into a CSV for download. It works perfectly fine until I have way too many rows and Chrome gives me the "aw snap" error page when I try to download the csv. How do I fix this?
var csvContent = "data:text/csv;charset=utf-8,";
data.forEach(function (infoArray, index) {
dataString = infoArray.join(",");
csvContent += dataString + "\n";
});
var encodedUri = encodeURI(csvContent);
var link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", "Data.csv");
link.click();
Chrome can only handle HREF's that are roughly 2 million characters long (or less).
You want to add the output to a Blob and then on that blob create an ObjectURL using URL.createObjectURL (MDN) and attach that to the href attribute of the anchor.
An example might be:
var csvContent = "";
data.forEach(function (infoArray, index) {
dataString = infoArray.join(",");
csvContent += dataString + "\n";
});
var blobdata = new Blob([csvContent],{type : 'text/csv'});
var link = document.createElement("a");
link.setAttribute("href", window.URL.createObjectURL(blobdata));
link.setAttribute("download", "Data.csv");
document.body.appendChild(link);
link.click();