Skip to content Skip to sidebar Skip to footer

Javascript, How Could We Read A Local Text File With Accent Letters Into It?

I have one doubt because I need to read a local file and I have been studying some threads, and I have seen various ways to handle it, in most of the cases there is an input file.

Solution 1:

It sounds like the file is encoded with ISO-8859-1 (or possibly the very-similar Windows-1252).

There's no BOM or equivalent for those encodings.

The only solutions I can see are:

  1. Use a (local) server and have it return the HTTP Content-Type header with the encoding identified as a charset, e.g. Content-Type: text/plain; encoding=ISO-8859-1

  2. Use UTF-8 instead (e.g., open the file in an editor as ISO-8859-1, then save it as UTF-8 instead), as that's the default encoding for XHR response bodies.

Solution 2:

  1. Put your text in an .html file with the corresponding content type, for example:

    <metahttp-equiv="Content-Type"content="text/html; charset="UTF-8">

    enclose the text between two tags ("####" in my example) (or put in a div)

  2. Read the html page, extract the content and select the text:

    window.open(url); //..var content = newWindow.document.body.innerHTML;
     var strSep="####";
     var x = content.indexOf(strSep);
     x=x+strSep.length;    
     var y = content.lastIndexOf(strSep); 
     var points=content.slice(x, y);
    

Post a Comment for "Javascript, How Could We Read A Local Text File With Accent Letters Into It?"