Javascript: Argument 1 of File constructor can't be converted to a sequence
Asked Answered
A

1

2

I'm trying to change the filename of a File object via javascript:

<html>
<head><title>test</title></head>
<body>
    <input type="file" id="file" onchange="__func(this)">
</body>

<script>
    function __func(target)
    {
        let file = target.files[0];
            file = new File(
                file.slice(0, file.size),
                file.name,
                {type: 'text/csv'}
            );

        console.log(file);
    }
</script>
</html>


Even if slice returns a Blob object, and File constructor does accept a Blob object as first parameter, I receive the following error:

Argument 1 of File constructor can't be converted to a sequence.

How can I solve it?

Acre answered 26/3, 2020 at 10:49 Comment(1)
What exactly you want to achieve?Tetrapod
T
2

File is itself a blob, so we can pass [file] similar to [blob] and type can be accessed by file.type

Try this

<html>
<head><title>test</title></head>
<body>
    <input type="file" id="file" onchange="__func(this)">
</body>

<script>
    function __func(target)
    {
        let file = target.files[0];
       file = new File([file], 'youRenamedIt.csv',{type:file.type});
        console.log(file);
    }
</script>
</html>
Tetrapod answered 26/3, 2020 at 11:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.