Javascript: File 构造函数的参数 1 无法转换为序列

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

我正在尝试通过 javascript:

更改 File 对象的文件名
<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>


即使 slice returns a Blob object, and File constructor does accept 一个 Blob 对象作为第一个参数,我也会收到以下错误:

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

我该如何解决?

文件本身就是一个blob,所以我们可以像[blob]一样传递[file],类型可以被file.type访问

试试这个

<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>