如何获取blob的名字
要获取blob的名称,您可以使用以下方法:
- 使用Blob对象的name属性:
var blob = new Blob(["Hello World!"], {type: "text/plain"});
var name = blob.name;
console.log(name); // returns undefined
在这种情况下,因为Blob对象没有指定名称,所以name属性返回undefined。
- 使用File对象的name属性:
var file = new File(["Hello World!"], "hello.txt", {type: "text/plain"});
var name = file.name;
console.log(name); // returns "hello.txt"
在这种情况下,因为File对象已指定名称,所以name属性返回文件名。
- 使用URL对象的pathname属性:
var blob = new Blob(["Hello World!"], {type: "text/plain"});
var url = URL.createObjectURL(blob);
var pathname = new URL(url).pathname;
var name = pathname.substring(pathname.lastIndexOf('/')+1);
console.log(name); // returns a unique identifier based on the Blob's contents
在这种情况下,因为Blob对象没有指定名称,所以我们可以使用URL.createObjectURL()方法创建一个URL对象。然后,我们可以使用URL对象的pathname属性获取Blob对象的路径,并使用JavaScript函数substring()获取Blob对象的名称。注意,由于Blob对象没有指定名称,因此获取的名称是基于Blob对象内容的唯一标识符
原文地址: https://www.cveoy.top/t/topic/hlfj 著作权归作者所有。请勿转载和采集!