skip to Main Content

So, I did this in my html code:

<script>
    let saveFile = () => {
        const search = document.getElementById('search');
        const email = document.getElementById('email');
        const password = document.getElementById('password');
        const tel = document.getElementById('tel');
        const check = document.getElementById('check');
        
        let data = 
            'r search: ' + search.value + ' rn ' + 
            'email: ' + email.value + ' rn ' + 
            'password: ' +password.value + ' rn ' + 
            'tel: ' + tel.value + ' rn ' +
            'check: ' + check.value + ' rn ';
        
        const textToBLOB = new Blob([data], { type: 'text/plain' });
        const sFileName = 'data.txt';

        let newLink = document.createElement("a");
        newLink.download = sFileName;

        if (window.webkitURL != null) {
            newLink.href = window.webkitURL.createObjectURL(textToBLOB);
        }
        else {
            newLink.href = window.URL.createObjectURL(textToBLOB);
            newLink.style.display = "none";
            document.body.appendChild(newLink);
        }

        newLink.click(); 
    }
</script>

I am showing you this code, which takes the values from the inputs and puts them into a .txt file that is downloaded, so you see what I used. I do not know if it is needed, but just to be safe. After the .txt file with the values downloads, I want to read the text from the downloaded file into my other html code. It has no real purpose, so that is why I want to do it like this. IT IS JUST FOR FUN. That is why I want to read the text, into the html code instead just taking the values. I am just curious.

2

Answers


  1. hi there actally the error you are seeing data back from the .txt file into an HTML documents because when you are reading the process is going to be a little complex due to browser security.they are designed like this so web pages cant access files.
    to achieve the functionality you can use which will let the user to select the file
    example

    Load

    Login or Signup to reply.
  2. Due to security concerns directly loading files from a script is forbidden.
    The only way to achieve what you want is to ask the user for some input file with an input to let him choose the file he wants to upload.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search