简体   繁体   English

如何使用FileReader检测'\\ n \\ r'

[英]How to detect '\n\r' with FileReader

I'm loading a file on my webapp and I use this function I found on a website to read it: 我正在Web应用程序上加载文件,并且使用在网站上找到的以下功能来读取该文件:

<script>        
    function readBlob() {
        var files = document.getElementById('files').files;

        if (!files.length) {
            alert('Please select a file!');
            return;
        }

        var file = files[0];
        var start = 0;
        var stop = file.size - 1;

        var reader = new FileReader();

        // If we use onloadend, we need to check the readyState.
        reader.onloadend = function(evt) {
            if (evt.target.readyState == FileReader.DONE) { // DONE == 2
                document.getElementById('byte_content').textContent = evt.target.result;
            }
        };

        var blob = file.slice(start, stop + 1);

        reader.readAsBinaryString(blob);

    }                                     
</script>

The reading works fine but it seems that the "\\n\\r" is not read and all my lines are stick together. 读取工作正常,但似乎未读取"\\n\\r"并且我所有的行都粘在一起。
Is there anything to change in this code to take account of '\\n\\r' ? 此代码有什么需要更改的地方,以考虑'\\n\\r'吗?

You can solve this problem with CSS only. 您只能使用CSS解决此问题。 Demo . 演示 MDN . MDN

#byte_content { white-space: pre}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM