如何生成和共享文件?

问题描述 投票:0回答:1

我想通过api web share共享一个csv文件,但我不想使用教程演示中的上载文件,而是要使用生成的文件,类似于this question中的操作。

我正在做的是

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));
javascript html web share
1个回答
0
投票

它必须是File对象,而不仅仅是Blob:

const file = new File( [ "foo bar" ], "foobar.txt" );

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

但是请注意,此file成员仅在level-2 specs中,而这只是一个草稿(在Chrome中的chrome://flags/#enable-experimental-web-platform-features下可以访问。

© www.soinside.com 2019 - 2024. All rights reserved.