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 Answers1

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.

stephenlcurtis
  • 180
  • 1
  • 7