How to get local file name using URL.createObjectURL?

I use createObjectURLto get image attributes like width and height. How to get local file name?

loadURL = function (event) {
    var output = document.getElementById('myimage'),
        output.src = URL.createObjectURL(event.target.files[0]);
    output.onload = function () {

        document.getElementById("urlinput").value = output. ? ? ? ? ?

        document.getElementById("width").value = output.width;
        document.getElementById("height").value = output.height;
    }
}

How do you get the file name from this? I tried filename, srcand name. I know that I can get the value from the corresponding one input type="file", but I want to get it from the object (output. ????? above), but how?

+4
source share
1 answer

URL.createObjectURL . URL- . img , event.target , .

loadURL = function (event) {
    var output = document.getElementById('myimage'),
        output.src = URL.createObjectURL(event.target.files[0]),
        fileName = event.target.files[0].name;
    output.onload = function () {

        document.getElementById("urlinput").value = output. ? ? ? ? ?

        document.getElementById("width").value = output.width;
        document.getElementById("height").value = output.height;
    }
}

Fiddle: http://jsfiddle.net/AtheistP3ace/h1pswvk1/

HTML:

<input id="test" type="file" />

JS:

document.getElementById('test').addEventListener('change',
    function (event) {
        alert(event.target.files[0].name);
    }
);

EDIT: , , , , , . , document.getElementById('myimage') img, . , src url , , img , src, , src . , , . , ? myimage?

+3

Source: https://habr.com/ru/post/1617471/


All Articles