Ste*_*tef 6 google-chrome navigator typescript progressive-web-apps
我正在构建 Angular8 PWA,并使用网络共享来共享文本,效果很好。\n自 2019 年 5 月起,Chrome 还支持\n文件共享
\n\n但是,尝试在 Typescript 中构建文件共享时遇到以下错误:
\n\nNotAllowedError:权限被拒绝
\n\nlet navigator: any;\nnavigator = window.navigator;\nconst title = "myTitle";\nlet data = {\n title: title,\n text: text,\n url: url,\n files: []\n};\nconsole.log(data);\n\nif (navigator.share) {\n fetch(url)\n .then(res => res.blob()) \n .then(file => {\n const fileName = data.text + ".mp3";\n const options = { type: "audio/mp3" };\n const newFile = new File([file], fileName, options);\n data.files.push(newFile);\n console.log(data);\n//lastModified: 1564912016680\n//lastModifiedDate: Sun Aug 04 2019 11:46:56 GMT+0200 (Mitteleurop\xc3\xa4ische //Sommerzeit) {}\n//name: "myName.mp3"\n//size: 40643\n//type: "audio/mpeg"\n//webkitRelativePath: ""\n if (navigator.canShare(data)) {\n navigator\n .share(data)\n .then(() => {})\n .catch(err => {\n console.error("Unsuccessful share " + err.message); //here is am getting the Permissions denied error\n });\n }\n });\nRun Code Online (Sandbox Code Playgroud)\n\n我不确定这是否是我获取文件(看起来不错)或调用 canShare 的方式。\n我在手机上使用 Chrome。以下小提琴适用于我的手机,但您需要选择一个文件。\n https://jsfiddle.net/ericwilligers/8cpuskqd/
\n\n我的共享功能位于一个按钮上,该按钮基本上包含要共享的文件的链接。
\n\n编辑
\n\n如果我将 data.files 从数组更改为对象,我会收到以下错误消息:
\n\nTypeError:无法在“Navigator”上执行“canShare”:迭代器 getter 不可调用。
\n\n编辑2
\n\n我创建了一个 codepen 来重现该问题:
\n\n\n这有效
webshare(url, text) {
let navigator: any;
navigator = window.navigator;
const title = "yourTitle";
let data = { files: [], text: text, url: url, title: title };
const options = { type: "audio/mp3" };
this.http
.get(url, {
responseType: "arraybuffer"
})
.subscribe(response => {
console.log(response);
let blob = new File([response], `${text}.mp3`, options);
data.files.push(blob);
console.log(data);
if (navigator.canShare(data)) {
navigator
.share(data)
.then(() => {})
.catch(err => {
console.error("Unsuccessful share " + err);
});
}
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5758 次 |
| 最近记录: |