javascript - 如何生成和共享文件?
问题描述
我想通过api web 共享共享一个 csv 文件,但不是像教程演示中那样使用上传的文件,而是使用生成的文件,类似于在这个问题中所做的。
我正在做的是
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));
解决方案
它需要是一个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
成员仅处于2 级规范中,这仍然只是一个草稿(可chrome://flags/#enable-experimental-web-platform-features
在 Chrome 中访问)。
推荐阅读
- wordpress - 显示来自另一个 CPT 的自定义字段
- javascript - 如何在 vanilla JS 中创建一个空状态的结果?
- javascript - Svelte、Netlify(部署失败)
- rabbitmq - 用于 AMQP1.0 的 RabbitMQ 铲子
- android - Android Kotlin - 序列化 mutableList
- python - 解决 streamlit python 中的“NonExistentTimeError: 2015-03-22 00:00:00”错误
- python - 为什么我的同情情节在中间有一个轴?
- asp.net - IIS Intranet 应用程序中的 Kerberos 身份验证问题 - 云与本地
- schema - 多种形式变体的数据库模式
- ruby-on-rails-6 - Rails 6.0 基于会话的身份验证,在客户端浏览器重新加载时丢失会话数据