
export excel with js with wrong file extension ".part"
I used code : document.location = 'data:application/vnd.ms-excel;base64,'+Base64.encode('the xml to generate the excel file');
to export excel from grid,
name of the file downloaded is xxxx.xls.part
if I change the extension to .xls, the file can be opened correctly.
what should I do to get the right file extension when download?
글쓴이 mileskunta 수정일시
선택된 해결법
".part" will be resolved once use FF20+.
You can set your file name as below document.getElementById("Your div id").download = 'NameYouWant.xls';
문맥에 따라 이 답변을 읽어주세요 👍 1모든 댓글 (6)
In which version have you checked.Try mozilla 20 and above. I faced the same issue it got solved when i used Mozilla 20 version
Suren2327,
we are using FF17, there are two problems, one is it has the extension ".part", another problem is that we cannot set the file's name, it will be an random string by default.
선택된 해결법
".part" will be resolved once use FF20+.
You can set your file name as below document.getElementById("Your div id").download = 'NameYouWant.xls';
Hi Suren2327,
have tried ,the extension is correct now, but about the file name, I am not downloading from a dom element, I actually created a String of hrml and download this html as an xml.
Great Suren2327,
I have tried your way, it works well, Thank you for giving me such a good solution.
here reference the solution in stack over flow: the url is:http://stackoverflow.com/questions/17126453/html-table-to-excel-javascript see the first answer.
You can use download attribute supported by modern browsera for a anchor element. First modify your HTML by adding an invisible anchor:
<a id="dlink" style="display:none;"></a>
<input type="button" onclick="tableToExcel('tablename', 'name', 'myfile.xls')" value="Export to Excel">
Notice also that the call to function tableToExcel now has 3rd parameter - where you specify file name.
Now use this modified code of your original function:
var tableToExcel = (function () {
var uri = 'data:application/vnd.ms-excel;base64,' , template = '{table}
document.getElementById("dlink").href = uri + base64(format(template, ctx)); document.getElementById("dlink").download = filename; document.getElementById("dlink").click();
} })()
Notice last 3 code lines: Instead of assigning URL to window - they assign it to the new anchor, then use new download attribute to force download as the given file name and then simple call click() method of the anchor.
Give it a try.