1

I want to share a csv file through the api web share, but instead of using an uploaded file as in the tutorial demos, I want to use a generated file, similar to what is done in this question.

What I'm doing is

navigator.share({
    files: [new Blob([data], {type: type})],
    title: 'Vacation Pictures',
    text: 'Photos from September 27 to October 14.',
  })
  .then(() => console.log('Share was successful.'))
  .catch((error) => console.log('Sharing failed', error));
Dharman
  • 30,962
  • 25
  • 85
  • 135
Jp_
  • 5,973
  • 4
  • 25
  • 36

1 Answers1

2

It needs to be a File object, not just a Blob:

const file = new File( [ "foo bar" ], "foobar.txt", {type: "text/plain" );

if( navigator.canShare && navigator.canShare( { files: [ file ] } ) ) {
  navigator.share({
    files: [ file ],
    title: 'Dummy text file',
    text: 'Some dummy text file',
  })
  .then( () => console.log( 'Share was successful.' ) )
  .catch( (error) => console.log( 'Sharing failed', error.message ) );
}
else {
  console.log( "can't share this" );
}

But note that this file member is only in the level-2 specs, which is still just a draft (accessible under chrome://flags/#enable-experimental-web-platform-features in Chrome).

Jp_
  • 5,973
  • 4
  • 25
  • 36
Kaiido
  • 123,334
  • 13
  • 219
  • 285
  • What is foo bar text here? What will come in this? – kb0000 May 14 '20 at 15:08
  • @kb0000 that's the file content, so this file will be a text file made of "foo bar", encoded in utf8 – Kaiido May 15 '20 at 00:30
  • So, when file is already being provided as foobar.txt then do we need to copy or send text (like foo bar here)? – kb0000 May 17 '20 at 09:41
  • @kb0000 if you already have a File object, then you can omit the first line (as long the variable holding it is called `file`.) – Kaiido May 17 '20 at 11:12