Black Leather
Black Leather

Reputation: 73

How to get path directory from FileReader()?

Hi i have these codes to read the file the user has uploaded:

function readURL(input) {
  if (input.files && input.files[0]) {
    var reader = new FileReader();

    reader.onload = function(e) {
      $('#myImg').attr('src', e.target.result);
    }

    reader.readAsDataURL(input.files[0]);
  }
}

And the output is a whole chunk of data:

enter image description here

Is there any way i can get the path from the data? for example C:\Users\blackLeather\Desktop

If no,is there another way to get the image directory without having to add into another folder?

Upvotes: 6

Views: 13257

Answers (3)

AlwaysLearning
AlwaysLearning

Reputation: 174

Like stated before you can't get the URL where the file lived, but you can create one.

function createFileURL() {
     var uploadedFile = document.getElementById("customFile").files[0];
     var reader = new FileReader();
     reader.readAsDataURL(uploadedFile);
     reader.onload = function () {
        return URL.createObjectURL(uploadedFile);
     }
  }

Upvotes: 1

Saleem
Saleem

Reputation: 29

Add this in element:

onchange="loadFile(event)
var loadFile = function(event) {
    var image = document.getElementById('output');
    image.src = URL.createObjectURL(event.target.files[0]);
};

Upvotes: 1

T.J. Crowder
T.J. Crowder

Reputation: 1075159

Is there any way i can get the path from the data?

No. None at all. That information is not provided to the JavaScript layer by the browser, for security reasons.

Upvotes: 9

Related Questions