Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
typescript中的navigator.canShare()权限被拒绝_Typescript_Google Chrome_Progressive Web Apps_Navigator - Fatal编程技术网

typescript中的navigator.canShare()权限被拒绝

typescript中的navigator.canShare()权限被拒绝,typescript,google-chrome,progressive-web-apps,navigator,Typescript,Google Chrome,Progressive Web Apps,Navigator,我正在构建一个Angular8 PWA,我使用webshare来共享文本,效果很好。 自2019年5月起,Chrome还支持 但是,尝试在Typescript中构建文件共享时遇到以下错误: NotAllowedError:权限被拒绝 let navigator: any; navigator = window.navigator; const title = "myTitle"; let data = { title: title, text: text, url: url,

我正在构建一个Angular8 PWA,我使用webshare来共享文本,效果很好。 自2019年5月起,Chrome还支持

但是,尝试在Typescript中构建文件共享时遇到以下错误:

NotAllowedError:权限被拒绝

let navigator: any;
navigator = window.navigator;
const title = "myTitle";
let data = {
  title: title,
  text: text,
  url: url,
  files: []
};
console.log(data);

if (navigator.share) {
  fetch(url)
    .then(res => res.blob()) 
    .then(file => {
      const fileName = data.text + ".mp3";
      const options = { type: "audio/mp3" };
      const newFile = new File([file], fileName, options);
      data.files.push(newFile);
      console.log(data);
//lastModified: 1564912016680
//lastModifiedDate: Sun Aug 04 2019 11:46:56 GMT+0200 (Mitteleuropäische //Sommerzeit) {}
//name: "myName.mp3"
//size: 40643
//type: "audio/mpeg"
//webkitRelativePath: ""
      if (navigator.canShare(data)) {
        navigator
          .share(data)
          .then(() => {})
          .catch(err => {
            console.error("Unsuccessful share " + err.message); //here is am getting the Permissions denied error
          });
      }
    });
我不确定这是我获取文件的方式(看起来不错)还是调用canShare。 我在手机上使用Chrome浏览器。下面的小提琴适用于我的手机,但您需要选择一个文件。

“我的共享”功能位于一个按钮上,该按钮基本上保持要共享的文件链接

编辑

如果将data.files从数组更改为对象,则会收到以下错误消息:

TypeError:未能在“Navigator”上执行“canShare”:迭代器getter不可调用。

edit2

我创建了一个代码笔来重现这个问题:

这起作用了

 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);
            });
        }
      });
  }

如果有人想使用fetch来利用async,您可以像下面这样做

constShareNow=async()=>{
让imageResponse=wait window.fetch('https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png');
让imageBuffer=等待imageResponse.arrayBuffer();
让fileArray=[新文件([图像缓冲区],“文件名”{
键入:“image/png”,
lastModified:Date.now()
})];
if(window.navigator&&window.navigator.canShare&&window.navigator.canShare({files:fileArray})){
navigator.share({
文件:fileArray,
标题:“标题”,
文本:“要显示的文本”
}).然后(()=>{
log('谢谢分享!');
})
.catch(控制台错误);
}
}

Web共享API
共享文件
使用此代码获取图像共享的共享选项。 请注意,navigation.share仅适用于HTTPS,不适用于HTTP服务器。 这是角度代码共享图像的示例。 我已将图像存储在assest/img文件夹中,请确保选择正确的图像url进行共享。

}

import { HttpClient } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
const navigator = window.navigator as any;

@Component({
  selector: 'app-image-post',
  templateUrl: './image-post.component.html',
  styleUrls: ['./image-post.component.css']
})
export class ImagePostComponent {

  constructor(private http: HttpClient) {}

  // This method shares the image as apost
  shareNow = async () => {
    console.log("insdie shareNow method....");
    if ('canShare' in navigator) {
      console.log("insdie if condition....");
      let img = 'assets/img/image-post-1.jpg';
      const share = async function () {
        try {
          const response = await fetch(img);
          const blob = await response.blob();
          const file = new File([blob], 'rick.jpg', { type: blob.type });
          await navigator.share({
            url: img,
            title: 'Image',
            text: 'Image',
            files: [file],
          });
          console.log("shared successfully....");
        } catch (err) {
          console.log(err.name, err.message);
        }
      };
      share();
    }
  };