0

In Edge extension I want to get all upload file name.Here is my code:

document.addEventListener('click', (event) => {
    const element = event.srcElement;
    const elementName = element.localName;
    if (elementName === 'input') {
        if (element.getAttribute('type') === 'file') {
            element.onchange = function (e) {
                console.log(e.target.value);
            }
        }
    }
});

But, this console.log is :

{"notifyType":"consoleItemLog","message":{"message":"","styles":"","hasFormatString":true,"targetId":"uid36","fileUrl":"js/contentscript.js","lineNumber":49,"columnNumber":17}}

I do not know how to get upload file name? And when I console.log(e), the onchange is permission denied.

enter image description here

thank you very much!

Nayantara Jeyaraj
  • 2,624
  • 7
  • 34
  • 63
王张军
  • 11
  • 3

1 Answers1

0

I can see that you want to use OnChange Event, But I don't understand where you want to use this event. It is better that you use the click event of button to get list of uploaded file. Below is a sample code.

<!DOCTYPE html>
<html>
<body>

<p>Click the button to create a File Upload Button.</p>
<input type="file" id="fileElementId" name="files[]" multiple/>
<button onclick="fun2()">Try it</button>

<script>

function fun2()
{
var inp = document.getElementById('fileElementId');
for (var i = 0; i < inp.files.length; ++i) {
  var name = inp.files.item(i).name;
  //alert("here is a file name: " + name);
  console.log("here is a file name: " + name);
}
}
</script>

</body>
</html>

Output: Click here to see the output:

Regards

Deepak

Deepak-MSFT
  • 10,379
  • 1
  • 12
  • 19
  • Thanks for your reply!It works well. But my question is in Microsoft Edge Browser extension(https://learn.microsoft.com/en-us/microsoft-edge/extensions/api-support).I want to write a Edge extension to get any upload file name.My code works well in chrome-extension. But not well in Edge browser extension. anyway, thank you very much. – 王张军 Sep 06 '18 at 09:28