0

I would like to read a file without a tag <input type="file" /> ( I have the path ex. c:\temp\file.txt ) and encode to base64 can anybody help me.

function retornabase64(nomArq) {

    var sPathh = '<%=sCaminhoArquivo%>'; //path: c:\temp\temp.txt
    var file =  sCaminho + "\\" + nomArq;
    var reader = new FileReader();
      
    reader.onload = function () {
        base64String = reader.result.replace("data:", "")
            .replace(/^.+,/, "");     
        imageBase64Stringsep = base64String;

    }
    reader.readAsDataURL(file);
    console.log('BaseLog: ',base64String);
    return base64String;
    }

1 Answer 1

1

As discussed in these answers, you cannot do this in modern browsers due to security concerns about access to the local filesystem.

One way to get around it (as covered in one of the answers) is to disable the browser security check, and use fetch to treat the local filesystem as an external URL. This approach is covered thoroughly here, but is not recommended for any website you'll be sharing with others.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.