我想通过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));
答案 0 :(得分:1)
它必须是File对象,而不仅仅是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" );
}
但是请注意,此file
成员仅在level-2 specs中,而该成员仍只是草稿(在Chrome中的chrome://flags/#enable-experimental-web-platform-features
下可以访问)。