Ionic 离子电容器在Android中打开文件不工作

euoag5mw  于 2022-12-16  发布在  Ionic
关注(0)|答案(1)|浏览(170)

我的应用程序是在Ionic 6与电容器。我应该有PDF和JPG文件在智能手机浏览器中打开。从Chrome它的工作在Android和iOS不。我该怎么办?

async openAttachments(attachment: UserAttachmentDTO) {

if (attachment.AttachmentType == AttachmentType.LINK) {
  window.open(attachment.FilePath, '_blank');
  return;
}

//Se è un FILE di tipo DICOM allora richiama l'URL per il DICOM VIEWER
if (attachment.UserAttachmentType == UserAttachmentTypeEnum.DICOM || attachment.FileExtension.toLowerCase().includes("dcm")) {
  this.openDicomAttachment(attachment);
  return;
}

if (!attachment.FileDataBase64 || !attachment.FileContentType) {
  let attachFromServer: AttachmentDTO = await this.attachmentService.getByID(attachment.FK_Attachment);
  attachment.FileDataBase64 = attachFromServer.FileDataBase64;
  attachment.FileContentType = attachFromServer.FileContentType;
}
console.log(attachment);

let extension = attachment.FileExtension.toLowerCase();
if (extension.includes("pdf") || extension.includes("png") || extension.includes("jpg") || extension.includes("txt") || extension.includes("rtf")) {
  let attachmentWindow = window.open("", '_blank');
  attachmentWindow.document.write("<iframe width='100%' height='100%' src='data:" + attachment.FileContentType + ";base64, " +
    encodeURI(attachment.FileDataBase64) + "'></iframe>");
  attachmentWindow.document.title = attachment.FileName;
}
else {
  let source = `data:${attachment.FileContentType};base64,${attachment.FileDataBase64}`;
  const link = document.createElement("a");
  link.href = source;
  link.download = `${attachment.FileName}.${attachment.FileExtension}`
  link.click();
}}
u0sqgete

u0sqgete1#

要在原生ionic应用中打开文件,你应该使用原生插件。试试file opener插件。如果不起作用,继续搜索合适的插件。每个插件都可能被弃用或在你的情况下不起作用。如果你找不到适合你的情况的插件,你必须用原生android和iOS代码自己写一个。试试capacitor plugins

相关问题