Javascript read file without using input

javascript read local file from path
javascript read file from input
javascript read text file into string
javascript read file from disk
javascript filereader
jquery read local text file
javascript file browser
javascript read text file line by line

I have this code and for a file to be converted into base64 I have to click on Choose file and then select it. I want to hardcode the file name so it is converted to base64 on page load.


var handleFileSelect = function(evt) {
  var files =;
  var file = files[0];

  if (files && file) {
    var reader = new FileReader();

    reader.onload = function(readerEvt) {
      var binaryString =;
      document.getElementById("base64textarea").value = btoa(binaryString);


  if (window.File && window.FileReader
      && window.FileList && window.Blob) {
        .addEventListener('change', handleFileSelect, false);
  } else {
    alert('The File APIs are not fully supported in this browser.');


    <label for="filePicker">Choose or drag a file:</label><br/>
    <input type="file" id="filePicker">
    <h1>Base64 encoded version</h1>
    <textarea id="base64textarea"
        placeholder="Base64 will appear here"
        cols="50" rows="15">

EDIT: Thank you for your answers, they were really helpful.

You simply can't do what you are trying to do. Setting the path for an input element through Javascript is not possible, as a security measure. Please check here: How to resolve the C:\fakepath?

How to read and parse a local file in Javascript/jquery, without using , Without user' intervention, any site can target random files from the user' machines if such feature is present. To avoid such a security breach, input file controls  Local files can be opened and read in the browser using the Javascript FileReader object. Reading a file from the local filesystem can be achieved using Javascript by : Choosing a file from the user's system through <input> file element.

You can launch chromium, chrome browser with --allow-file-access-from-files flag set, use fetch() of XMLHttpRequest() to request file from local filesystem.

.then(response => response.arrayBuffer())
.then(ab => {
  // do stuff with `ArrayBuffer` representation of file
.catch(err => console.log(err));

See also Read local XML with JS

Read Text Files Using the JavaScript FileReader, Read Text Files Using the JavaScript FileReader perform fairly sophisticated client-side processing on the content of local files without having to upload them to a server. Gaining Access to Files using the File Input Control. (Think <input type="file" multiple> or dragging a directory of files from the desktop). Blob - Allows for slicing a file into byte ranges. When used in conjunction with the above data structures, the FileReader interface can be used to asynchronously read a file through familiar JavaScript event handling. Thus, it is possible to monitor the progress of a read, catch errors, and determine when a load is complete.

The File API is not good to read local files without user intervention, but the Web API is (of course, within its limitations, like not working in Chromium without explicitly enabling access to local files and whatnot).

So, here it is, in case someone else needs a working example of how to load a local file without user intervention, i.e., without requiring user to push any INPUT button (but still giving the user a means to abort the loading).

Parameters: file name, request type (text, blob etc.), MIME type and a function to be executed after the file is completely loaded. File is loaded in variable X, which is then used to populated an object.

To abort the file reading, just click on the progress bar (also, just an example, not essential for the program to work). Because it is asynchronous, as many files as wanted may be read at the same time (one progress bar is created for each file).

I only created examples for a text file and a video, but it should work with any kind of files.

<meta charset="utf-8"/>
<script type="text/javascript">
function LoadFile(FileName,RespType,FileType,RunMe)
{   var AJAXFileReader=new XMLHttpRequest();

    // Creates new progress bar.
    var ProgressBar=CreateSVGProgBar("ProgressBars");

        function FRProgress(AJAXFREvt)
        {   // Calculate progress.
            var X=-1;

            if (AJAXFREvt.lengthComputable)


    AJAXFileReader.addEventListener("error",function FRFailed()
        {   // This will be executed if an error occurs.

    AJAXFileReader.addEventListener("timeout",function FRTimeOut()
        {   // This will be executed if the reading times out.
            console.log("File reading timed out!");

        function FRCancel()
        {   // This will confirm reading was aborted.
            console.log("File reading cancelled by user!");

        function KillMe()
        {   // Adds an abort command to the object.
            if (AJAXFileReader.readyState!=4)
            {   console.log("Aborting file reading...");

        function Finished()
        {   // When reading is finished, send data to external function.
            if ((this.readyState==4)&&(this.status==200))
            {   ShowProgressBar(ProgressBar,FileName,100);
    AJAXFileReader.timeout=10000; // Setting time-out to 10 s.


function CreateSVGProgBar(AnchorId)
{   // Creates new SVG progress bar.
    var Parent=document.getElementById(AnchorId);
    var NewSVG=document.createElementNS("","svg");
    NewSVG.setAttribute("viewBox","0 0 102 22");
    return NewSVG;

function ShowProgressBar(E,N,X)
{   // Show progress bar.
    var P=X<0?"???":X+"%";

    E.innerHTML="<text x=\"50\" y=\"16\" font-size=\"12\" fill=\"black\" stroke=\"black\" text-anchor=\"middle\">"+N+"</text><rect x=\"1\" y=\"1\" width=\""+X+"\" height=\"20\" fill=\""+(X<100?"#FF0000":"#0000FF")+"\" stroke=\"none\"/><rect x=\"1\" y=\"1\" width=\"100\" height=\"20\" fill=\"none\" stroke=\"black\" stroke-width=\"1\"/><text x=\""+X/2+"\" y=\"16\" font-size=\"12\" fill=\"black\" stroke=\"black\" text-anchor=\"middle\">"+P+"</text>";

function TracerOn(X)
{   // This will be executed after the file is completely loaded.

function PlayIt(X)
{   // This will be executed after the file is completely loaded.
    var blob_uri=URL.createObjectURL(X);

function Startup()
{   // Run after the Page is loaded.


<body onload="Startup()">

<div id="ProgressBars"></div>

<div id="Tron">Text...</div>

<video id="MagicalBox" width="400" controls>Video...</video>


Can not using html5 file api to Read file directly without file dialog , Can not using html5 file api to Read file directly without file dialog #3227 I think maybe nw.js could allow developer directly read file by change 's special You can get the file path from input and read it by "fs" with NodeJS  Of course, you can always use Modernizr too. The JavaScript FileList and File Objects. Whether you include the multiple attribute or not, the file input always returns a FileList object, which is a simple array of individually selected files from the underlying system. Like any array, it is zero based, so files[0] gets the first one.

Using files from web applications, This selection can be done by either using an HTML input type="file" element or by drag and drop. name: The file's name as a read-only string. To allow opening the file picker without using JavaScript (the click() method),  Javascript read file without using input. I have this code and for a file to be converted into base64 I have to click on Choose file and then select it. I want to hardcode the file name so it is converted to base64 on page load.

[Solved] Javascript read data froma txt file, I believe fopen is a Java function not Javascript. access the file without the user first selecting it via an file input control. The following article explains how this can be achieved with Javascript using the file input control. The HTML5 File API allows you to create applications that let the user interact with files locally. Basically, you can load files and render them in the browser without actually having to upload the files. 3 Main HTML5 File Objects. There are three main objects that you need to know about to work with files locally:

How to open a local file with JavaScript?, The HTML5 FileReader facility does allow you to process local files, but these MUST be selected by the user, you Here is an example using FileReader: Examples of using JavaScript to access and manipulate HTML input objects. Button Object Disable a button Find the name of a button Find the type of a button Find the value of a button Find the text displayed on a button Find the id of the form a button belongs to

  • I have a feeling that the browsers are not going to allow you to arbitrarily load up a file from the users' filesystem. Some events can only be generated from direct user input.
  • It looks like you can't even set the value for <input type="file"> elements from javascript. See Notes: File Inputs.
  • can I, for example, use this script to display a local image?