我正在尝试通过 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>
即使切片
returns Blob
对象和 File
构造函数 does accept将 Blob
对象作为第一个参数,我收到以下错误:
Argument 1 of File constructor can't be converted to a sequence.
如何解决?
最佳答案
File本身就是一个blob,所以我们可以像[blob]一样传递[file],并且可以通过file.type来访问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>
关于Javascript:文件构造函数的参数 1 无法转换为序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60865555/