mag*_*a42 3 javascript jquery progressive-web-apps
我有一个从服务器生成的 pdf 文件,我想允许用户通过 navigator.share 共享这个文件。是否可以共享文件而不是 URL?
navigator.share({
title: 'Web Fundamentals',
text: 'Check out Web Fundamentals — it rocks!',
url: 'https://developers.google.com/web',
})
.then(() => console.log('Successful share'))
.catch((error) => console.log('Error sharing', error));
Run Code Online (Sandbox Code Playgroud)
Chrome 93.0.4570.0 现在支持通过 Web Share 共享 PDF 文件。请参阅https://chromiumdash.appspot.com/commit/7d30d42a018d4aa76fca2896f2903d892b5f5284和https://bugs.chromium.org/p/chromium/issues/detail?id=1006055
shareButton.onclick = async () => {
const response = await fetch("https://example.com/files/hello.pdf");
const buffer = await response.arrayBuffer();
const pdf = new File([buffer], "hello.pdf", { type: "application/pdf" });
const files = [pdf];
// Share PDF file if supported.
if (navigator.canShare({ files })) await navigator.share({ files });
};
Run Code Online (Sandbox Code Playgroud)
Chrome for Android (75+) 现在支持此功能(称为 Web Share API Level 2),希望其他浏览器也能很快效仿。
此处提供演示。
| 归档时间: |
|
| 查看次数: |
2984 次 |
| 最近记录: |