当读取二进制文件时,我确实认识到了,
我确实尝试使用base64,当我尝试使用以下语法下载文件时,它会给我一个错误。
<a href="data:text/plain;base64,VGVzdCBEb2N1bWVudA%3D%1E" download="sample text.txt">download</a>
生成上述代码的脚本是
<script>
var filecontent = binaryAgent("010101100100011101010110011110100110010001000011010000100100010101100010001100100100111000110001011000100101011101010110011101010110010001000001001111010011110");
download("sample text.txt", filecontent, "data:text/plain;base64,");
function binaryAgent(str) {
// Removes the spaces from the binary string
str = str.replace(/\s+/g, '');
// Pretty (correct) print binary (add a space every 8 characters)
str = str.match(/.{1,8}/g).join(" ");
var binString = '';
str.split(' ').map(function(bin) {
binString += String.fromCharCode(parseInt(bin, 2));
});
return binString;
}
function download(filename, text, encodedType) {
var element = document.createElement('a');
element.setAttribute('href', encodedType + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
</script>
这两种类型有什么区别?即使是基于文本的文件,我也不能使用base64吗?
答案 0 :(得分:1)
您刚刚生成了错误的base64,请替换
data:text/plain;base64,VGVzdCBEb2N1bWVudA%3D%1E
与
data:text/plain;base64,VGVzdCBEb2N1bWVudA