navigator.share(file) doesnt work for audio files

292 views Asked by At

I have this working function:

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

It works perfectly for txt files, videos and images, but not for audio files (e.g .mp3 file). when i log the file this is its properties:

 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

And in the console the error is: Sharing failed DOMException: Permission denied

Can someone please help me solve this problem? why is Permission denied?

Note: my website is served on HTTPS.

1

There are 1 answers

0
DenverCoder9 On

Your implementation is correct, but you’re running into a security restriction. The spec explains this in step 9 of the processing algorithm for the share() method:

If a file type is being blocked due to security considerations, return a promise rejected with a "NotAllowedError" DOMException.

Now you can argue of course why .mp3 files would be a security issue (I don't know the answer). Ideally you do this in a new.crbug.com.