【问题标题】:Ionic 5 File opener Issue is file not foundIonic 5 File opener 问题是找不到文件
【发布时间】:2021-08-20 10:22:14
【问题描述】:

我正在使用 Ionic 和 Angular 并制作一个应用程序。在我的应用中,我使用沙发数据库作为下载图片。

我正在使用 Android 手机。

我正在尝试在文件打开器的帮助下下载我的文件,我得到了 ->

(状态:9 消息:未找到文件)

   <ion-item mode=md class="input-group ion-no-padding viewFile" lines=none *ngIf="common.isDocumentFill">
     <span (click)="doc(fc)">View File</span>
    </ion-item>

//功能

doc(objFC) {

    let obj = {
      ServiceId: 1,
      CouchDBDocId: objFC.CouchDBDocId,
      DocumentName: objFC.FileName
    }
    this.api.getPdf(obj).subscribe((data: any) => {
     // debugger
      let blob = new Blob([data], { type: data.type });

      var downloadURL = window.URL.createObjectURL(data);
      var types = data.type
      this.down(downloadURL, types)
    }); 
 }
  down(filepath, mimeType) {
    this.fileOpener.open(filepath, mimeType)
     .then(() =>
     console.log('File is opened')
      )
     .catch(e => console.log('Error opening file', e)); 
    }

   

和服务

 getPdf(obj) {

    // debugger
    const httpOptions = {
      responseType: 'blob' as 'json'
    };

    return this.http.post(this.url + "DocumentDetails/DownloadFile", obj, httpOptions);
  } 

【问题讨论】:

    标签: javascript angular typescript ionic-framework ionic5


    【解决方案1】:

    请安装所需的插件

    1.import { FileOpener } from '@ionic-native/file-opener/ngx';    
    2.import { File } from '@ionic-native/file/ngx';
    

    在构造函数中创建实例

      constructor(
             private fileOpener: FileOpener,
             private file: File
             )
    

    把这个函数放在你的 ts 文件中

    openPDF (stringBase64PDF) {
                    debugger
                    fetch('data:application/pdf;base64,' + stringBase64PDF, {
                        method: "GET"
                    })
                    .then(res => res.blob()).then(blob => {
                      console.log("created blob");
                      this.file.createFile(this.file.dataDirectory, 'temp.pdf', true)
                      .then(() => {
                        console.log("file created");
                        this.file.writeFile(this.file.dataDirectory, 'temp.pdf', blob, { replace: true })
                        .then(res => {
                          console.log("file writed");
                          this.fileOpener.open(res.toInternalURL(), 'application/pdf')
                          .then((res) => {
                            console.log('file opened')
                          }).catch(err => {
                            console.log('open error')
                          });
                        }).catch(err => {
                          console.log('write error')     
                        });
                      }).catch(() => {
                        console.log("create error");
                      })
                      
                    }).catch(err => {
                      console.log('blob error')
                    });
                  }
    

    放入html文件

      <ion-item (click)="openPDF(base64pdf)">
                 <ion-label>Click here to open pdf file</ion-label>
                </ion-item>  
    

    【讨论】:

      【解决方案2】:

      尝试创建类似的 URL

      var downloadURL = (window.webkitURL || window.URL).createObjectURL(data);
      

      浏览器可能不支持window.URL -> read here
      说到手机,你是指 iOS 设备还是 Android 设备?

      【讨论】:

      • 我有 blob 对象,问题是`link.click`在手机中不起作用。
      • 请定义电话是什么意思?哪个操作系统?
      • 我的手机安卓
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-29
      • 2020-05-06
      • 2021-12-09
      相关资源
      最近更新 更多