Place . txt local file inside a local page

Asked

Viewed 552 times

0

Eae, y'all.
I want to warn you first that I don’t know much about java and these kinds of code, so I hope I don’t have any answers, but come on.
Next, what I need to do, for a project, is to use the text that is inside a ". txt" file and put it as text in an HTML page.
I have been researching, and from what I understand, there is no way to do this because the file is local (file://) and is not hosted on a server.
Is there any way around this? This project will not be uploaded. It will not go online, the only person who will open it is me, on my PC.
But then, in case it’s not possible, I need help with the code to make it work online.

HTML:

<div id="printed"></div>

CSS:

var text_print = new XMLHttpRequest();
        text_print.open("GET", "http://www.textfiles.com/etext/FICTION/2000010.txt", true);
        text_print.onload = function (){
            console.log( text_print.responseText.slice(0, text_print.responseText.indexOf("\n")) ); };
        text_print.send();

    document.getElementById("printed").innerHTML=text_print;

Needless to say, it didn’t work, did it? Hahaha
Like I said, I don’t know java and shovel, so I did something similar on the Internet and I ended up with this code there, but it doesn’t work.
I don’t think I need to explain, but what I need is for the text in the "2000010.txt" file to become the div with "printed id".
Thank you.

  • 1

    You can use Apache to create your own server on your machine so you can make HTTP requests as desired.

  • Hello Anderson! First thank you for the reply. I searched here and installed XAMPP. The file has this "http:/localhost/test/test.txt" URL. I would need to know now about the code as the above code still doesn’t work. = / Thanks.

  • 1

    Fix: It worked. I had left the page outside the local server, so this prevented the page from fetching the file. I put them both in the same folder on the localhost, Document.getElementByID inside the onload, deleted the console.log and it worked. = ) Thanks!

  • found this but could not make it work here. http://www.diogosouza.com.br/2016/01/como-configurar-flag-allow-file-access.html

  • Here only in Google Chrome does not work, IE asks if it allows open blocked content, Safari, Firefox and Microsoft Edge works. And with you alone in Mozila? Strange!!!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.