-2

Is there a way to download a div tag content using javascript or jquery into a word document?

  • Did you checked this http://stackoverflow.com/questions/11810212/download-a-div-to-doc-or-docx-using-js – PSK Jun 24 '14 at 04:08
  • Can't be done with js/jquery alone. It will need server side language too. Have you tried out anything? – asprin Jun 24 '14 at 04:08

2 Answers2

4

You can try this :(you can pass the parameter of mime type as you want in the function)

function downloadDiv(filename, elementId, mimeType) {
    var elementHtml = document.getElementById(elementId).innerHTML;
    var link = document.createElement('a');
    mimeType = mimeType || 'text/plain';

    link.setAttribute('download', filename);
    link.setAttribute('href', 'data:' + mimeType  +  ';charset=utf-8,' + encodeURIComponent(elementHtml));
    link.click(); 
}

var fileName =  'divContents.html';

HTML

<div id="divContent">
    <span>Some html text here</span>
</div>

<a href="#" id="downloadButton">Download the html</a>


$('#downloadButton').click(function(){
    downloadDiv(fileName, 'divContent','text/html');
});
SoftSan
  • 2,482
  • 3
  • 23
  • 54
0

I believe you should go for a server-side scripting language. In PHP, you could use the following header:

header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment;Filename=document_name.doc");

Detailed example

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
Manwal
  • 23,450
  • 12
  • 63
  • 93