0

i want to open a local file by Patch in javascript client-side . this is my code below -im using xmlhttprequest method :

<script type='text/javascript'>
    function readfilebypatch(file)
    {
        var rawfile  = new XMLHttpRequest();
        rawfile.open("GET" , file , true);
        var all ;
        alert("in");
        rawfile.onreadystatechange = function ()
        {
            if ( rawfile.status === 200 || rawfile.status == 0)
            {
                all = rawfile.responseText;
                alert(all);
            }
        }
       //rawfile.send('0');
    }
    readfilebypatch("file:///c:/users/acer/desktop/tavasoli.txt");
</script>

so i got this error after in alert :

ccess to XMLHttpRequest at 'file:///C:/users/acer/desktop/tavasoli.txt' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

andthis warning :

[Violation] 'readystatechange' handler took 1241ms

and this one :

BASE64.HTML:32 GET file:///C:/users/acer/desktop/tavasoli.txt net::ERR_FAILED

so i know my browser ( chrome ) is blocking these kind of requests and i have to do this : https://www.chrome-allow-file-access-from-file.com/windows.html but this doesnt work . Note: is there other way to open file in javascript client side ? thanks!

Rob
  • 14,746
  • 28
  • 47
  • 65
  • No, there is not. An XHR request to a `file:` URL does not make sense. An XHR implies that an HTTP server will respond, but `file:` URLs do not work that way. – Pointy Mar 09 '20 at 14:00
  • i think here it is working : https://stackoverflow.com/questions/14446447/how-to-read-a-local-text-file @pointy – Compo Start_up Mar 09 '20 at 14:02

0 Answers0