Search code examples
angularfilestream

reading file contents from host PC


I'm currently designing a simple log-in and chat application in Angular.

in Order to complete step 2 of a users log-in process, the API requires that I Provide a certain user ID.

This user ID is located in a local file on the users PC, more specifically in C:\Users\name\AppData\Roaming\appfolder (its a .dat file.)

I need to locate that file and read it's contents. I also do not want to prompt the user to do this themselves, this should happen "in the background"

how can I do this in AngularTs?

I looked at this question Angular 2 / Angular 4 : How can i access file from the local system location? but I'm not sure I'm on the right track as I don't want to retrieve any files from the server.

any help would be appreciated!!


Solution

  • Maybe someone can provide a better answer, but without prompting the user to select the file, you can't access a file on their system. It's not an Angular problem. The Web Browser won't let Angular (and Javascript) for security reasons.

    One solution:

    1. Prompt the user to select the file the first time.
    2. Read the File content to get client id.
    3. Save the client id in browser local storage.
    4. During logins, your Angular code reads browser local storage and automatically sends the client id.

    To read the file content after selection, you can try using FileReader.

    readFile(file: File) {
        var reader = new FileReader();
        reader.onload = () => {
            console.log(reader.result);
        };
        reader.readAsText(file);
    }