6

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?

Kunal Mukherjee
  • 5,775
  • 3
  • 25
  • 53
Black Leather
  • 73
  • 1
  • 2
  • 11

3 Answers3

9

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.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

Add this in element:

onchange="loadFile(event)
var loadFile = function(event) {
    var image = document.getElementById('output');
    image.src = URL.createObjectURL(event.target.files[0]);
};
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
Saleem
  • 29
  • 1
  • 4
  • This does not answer the question. What was asked for is impossible as T.J. Crowder correctly stated. – ahuemmer Aug 01 '22 at 16:22
0

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);
     }
  }
AlwaysLearning
  • 164
  • 1
  • 10