Cordova FileTransfer下载。仅限IOS的错误代码1:"无法创建目标文件"

时间:2018-03-19 14:03:43

标签: angular ionic-framework file-transfer

我使用FileTransfer和FileOpener下载然后在Ionic应用程序中查看文件,使用本机文件打开器查看文件。

使用IOS,当我尝试下载特定文件(在本例中为PDF)时,我收到错误:

{"代码":1, ... "无法创建目标文件"}

这适用于某些文档(大小相同),而其他文档也能正常工作。

  viewFile() {
this.analytics.trackEvent('document', 'view/download', this.document.originalFileName);
let fileName = this.document.originalFileName;
let filePath = `${this.file.dataDirectory}downloads/${fileName}`;
this.fileTransfer.download(this.document.fileUrl, filePath)
  .then(file => {
    this.openFile(filePath, fileName);
    this.analytics.trackEvent('document', 'view/download', this.insightId);
  })
  .catch(error => {
    console.log(`error: ${JSON.stringify(error)}`);
  });
  this.close();

}

1 个答案:

答案 0 :(得分:1)

我遇到的问题是它不接受空格。

我必须对fileName进行URI编码和解码(如果它有一个空格)。

现在可以使用以下内容:

viewFile() {
    this.analytics.trackEvent('document', 'view/download', this.document.originalFileName);
    let fileName = encodeURI(this.document.originalFileName);
    let filePath = "${this.file.dataDirectory}downloads/${fileName}";
    this.fileTransfer.download(this.document.fileUrl, filePath)
      .then(file => {
        this.openFile(filePath, fileName);
        this.analytics.trackEvent('document', 'view/download', this.insightId);
      })
      .catch(error => {
        console.log("error: ${JSON.stringify(error)}");
      });
      this.close();
  }

  openFile(filePath, fileName) {
    let mimeType = mime.lookup(fileName);
    this.fileOpener.open(decodeURI(filePath), mimeType)
      .catch(error => {
        if (error.status === 9) {
          this.messageService.sendMessage(new ErrorMessage("No viewer found for this file type"));
        }
        console.log("error: ${JSON.stringify(error)}");
      });
  }