navigator.share(file)不适用于音频文件



我有这个工作函数:

export const shareFile = (message: File) => {
const nav = window.navigator as any ;
const data = { files: [message], text: '' };
if (nav.canShare && nav.canShare(data)) {
navigator.share(data)
.then(() => console.log('Share was successful.'))
.catch(error => console.log('Sharing failed', error));
} else {
console.log(`Your system doesn't support sharing files.`);
}
};
};

它适用于txt文件、视频和图像,但不适用于音频文件(如.mp3文件(。当我记录文件时,这是它的属性:

file:
data: File
lastModified: 1619510595837
lastModifiedDate: Tue Apr 27 2021 11:03:15 GMT+0300 (Israel Daylight Time) {}
name: "feature-completion-1.mp3"
size: 4015
type: "audio/mpeg"
webkitRelativePath: ""
__proto__: File

在控制台中,错误为:Sharing failed DOMException: Permission denied

有人能帮我解决这个问题吗?为什么许可被拒绝?

注意:我的网站使用HTTPS。

您的实现是正确的,但您遇到了安全限制。规范在share()方法的处理算法的步骤9中对此进行了解释:

如果出于安全考虑,文件类型被阻止,则返回一个用"NotAllowedError"DOMException拒绝的promise

现在,您当然可以讨论为什么.mp3文件会是一个安全问题(我不知道答案(。理想情况下,你可以在一个新的.crbug.com.中这样做

最新更新