I try to create a CSV-File download with Javascript.
We need to export Data from our Website to a 3rd party Program, the creation and download works pretty well. There is just one Problem, I need the CSV-File encoded in ANSI (Windows-1252) - the 3rd Party Program is very old and can't understand Multi-Byte Encodings. My Files are served in UTF-8 and I found no way until now to convert the File to ANSI, I get always UTF-8-Content...
The current hotfix is to open the file and manually change the encoding to ANSI, but thats not nice and for some in my Company difficult to do (sometimes they forget it, because they are not really fortable with PCs).
I want a File that's ready to use in ANSI, not in UTF-8. I can convert the File in PHP, it has the correct encoding and content, but I have no write Access on the Server, thats why I need to download the File dynamic with AJAX.
I used a solution like him on Stackoverflow:
But I get UTF-8 as Content.
I went a step back and try to convert with JavaScript on a very simple page, but I got UTF-8 as well...:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html" charset="utf-8" />
<title>My File-Download</title>
<script type='text/javascript' src='encoding.js'></script>
<script type='text/javascript' src='encoding-indexes.js'></script>
<script type='text/javascript' src='FileSaver.min.js'></script>
<script type='text/javascript'>
<!--
/**
* Downloads the Text as File
*
* @param {string} filename - Name of the File
* @param {string} text - Content of the File
*/
function downloadExportFile(filename, text) {
var encodedText = new TextEncoder("windows-1252", {NONSTANDARD_allowLegacyEncoding: true}).encode([text]);
var blob = new Blob([encodedText], {type: 'text/plain;charset=windows-1252;'});
saveAs(blob, filename);
}
// -->
</script>
</head>
<body>
<a href="#" onclick="downloadExportFile('export.csv', 'Dragicevic,Peter,,1,Straße 4,21027,Hamburg,43,,,,,,,,');">Download Windows-1252 CSV-File</a>
</body>
</html>
I didn't found much about convert UTF-8 to ANSI (but in the other direction are tons of solutions). Did anybody know a solution to get a ANSI (Windows-1252)-File with JavaScript?
I try to create a CSV-File download with Javascript.
We need to export Data from our Website to a 3rd party Program, the creation and download works pretty well. There is just one Problem, I need the CSV-File encoded in ANSI (Windows-1252) - the 3rd Party Program is very old and can't understand Multi-Byte Encodings. My Files are served in UTF-8 and I found no way until now to convert the File to ANSI, I get always UTF-8-Content...
The current hotfix is to open the file and manually change the encoding to ANSI, but thats not nice and for some in my Company difficult to do (sometimes they forget it, because they are not really fortable with PCs).
I want a File that's ready to use in ANSI, not in UTF-8. I can convert the File in PHP, it has the correct encoding and content, but I have no write Access on the Server, thats why I need to download the File dynamic with AJAX.
I used a solution like him on Stackoverflow: https://stackoverflow./a/22089405/5092608
But I get UTF-8 as Content.
I went a step back and try to convert with JavaScript on a very simple page, but I got UTF-8 as well...:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html" charset="utf-8" />
<title>My File-Download</title>
<script type='text/javascript' src='encoding.js'></script>
<script type='text/javascript' src='encoding-indexes.js'></script>
<script type='text/javascript' src='FileSaver.min.js'></script>
<script type='text/javascript'>
<!--
/**
* Downloads the Text as File
*
* @param {string} filename - Name of the File
* @param {string} text - Content of the File
*/
function downloadExportFile(filename, text) {
var encodedText = new TextEncoder("windows-1252", {NONSTANDARD_allowLegacyEncoding: true}).encode([text]);
var blob = new Blob([encodedText], {type: 'text/plain;charset=windows-1252;'});
saveAs(blob, filename);
}
// -->
</script>
</head>
<body>
<a href="#" onclick="downloadExportFile('export.csv', 'Dragicevic,Peter,,1,Straße 4,21027,Hamburg,43,,,,,,,,');">Download Windows-1252 CSV-File</a>
</body>
</html>
I didn't found much about convert UTF-8 to ANSI (but in the other direction are tons of solutions). Did anybody know a solution to get a ANSI (Windows-1252)-File with JavaScript?
Share edited May 30, 2017 at 14:03 Petschko asked May 23, 2017 at 13:11 PetschkoPetschko 1783 silver badges16 bronze badges 1- 1 JavaScript does not have native conversion functions; you absolutely need a third-party library and library remendations are explicitly off-topic here. Whatever, since you're in a browser you have to stick to pure JavaScript implementations but, performance apart, there're some to choose from, such as iconv-js or iconv-lite. – Álvaro González Commented May 30, 2017 at 15:08
1 Answer
Reset to default 3I hope it helps!
Polyfill for the Encoding Living Standard's API
Download both files encoding-indexes.js and encoding.js
HTML sample:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script>
window.TextEncoder = window.TextDecoder = null;
</script>
<script type="text/javascript" src="encoding-indexes.js"></script>
<script type="text/javascript" src="encoding.js"></script>
<script>
function BlobDownload(Filename, Bytes, Mimetype) {
var filData = new Blob(Bytes, { type: Mimetype });
if (window.navigator && window.navigator.msSaveOrOpenBlob) { // for IE
window.navigator.msSaveOrOpenBlob(filData, Filename);
} else { // for Non-IE (chrome, firefox etc.)
var a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";
var filUrl = URL.createObjectURL(filData);
a.href = filUrl;
a.download = Filename;
a.click();
a.remove();
}
};
function download() {
var bytes = new TextEncoder("windows-1252", { NONSTANDARD_allowLegacyEncoding: true }).encode("Eu não tenho mais dúvidas");
BlobDownload("test_windows1252_encoding.txt", [bytes]);
BlobDownload("test_windows1252_encoding.csv", [bytes], "text/csv");
}
</script>
</head>
<body>
<button onclick="download()">Download</button>
</body>
</html>
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744879869a4598773.html
评论列表(0条)