err*_*ode 6 javascript capacitor capacitor-plugin
我需要分享一个 PDF 文件。我使用 CapacitorJS 来实现本机功能。
let shareRet = await Share.share({
title: 'See cool stuff',
text: 'Really awesome thing you need to see right meow',
url: 'http://ionicframework.com/',
dialogTitle: 'Share with buddies'
});
Run Code Online (Sandbox Code Playgroud)
这是从例子中得来的。但我的数据是 base64 字符串。有没有办法以附件形式分享?
这段代码适用于 iOS 和 Android:
import { Directory, Filesystem } from '@capacitor/filesystem';
import { Share } from '@capacitor/share';
function share(fileName: string, base64Data: string) {
return Filesystem.writeFile({
path: fileName,
data: base64Data,
directory: Directory.Cache
})
.then(() => {
return Filesystem.getUri({
directory: Directory.Cache,
path: fileName
});
})
.then((uriResult) => {
return Share.share({
title: fileName,
text: fileName,
url: uriResult.uri,
});
});
}
Run Code Online (Sandbox Code Playgroud)
此代码将共享图像,尝试使用它来编辑它jspdf以创建和共享 pdf
import domtoimage from 'dom-to-image';
import { Share } from '@capacitor/share';
import { Filesystem, Directory, Encoding } from '@capacitor/filesystem';
import { Capacitor } from '@capacitor/core';
import { Camera, CameraResultType } from '@capacitor/camera';
async shareImage() {
let receiptName = 'Receipt N:' + this.invoice?.invoiceNumber + '.png';
const div = this.screenshotElement.nativeElement;
const divHeight = div.clientHeight;
const divWidth = div.clientWidth;
const options = { background: '#ffffff', width: divWidth, height: divHeight };
await Filesystem.requestPermissions();
let base64Data = await domtoimage.toPng(div, options);
// browser download
this.downloadImgElement.nativeElement.src = base64Data;
this.downloadLinkElement.nativeElement.href = base64Data;
this.downloadLinkElement.nativeElement.download = receiptName;
this.downloadLinkElement.nativeElement.click();
// device shareing
await Filesystem.writeFile({
path: receiptName,
data: base64Data,
directory: Directory.Cache
});
let fileResult = await Filesystem.getUri({
directory: Directory.Cache,
path: receiptName
});
let imageLink = Capacitor.convertFileSrc(fileResult.uri);
Share.share({
title: receiptName,
text: receiptName,
url: fileResult.uri,
})
.then(() => console.log('Successful share'))
.catch((error) => console.log('Error sharing ::: ', error));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5089 次 |
| 最近记录: |