Ionic 4-HTTP GET下载的Blob /图像数据未加载/损坏

时间:2019-12-10 09:53:01

标签: php angular ionic-framework blob ionic4

我正在使用以下代码从Web服务器下载文件:

const fileExt = this.attachments[index].filename.split(".").pop();
const mimeType = this.globalsService.getMimeType(fileExt);
const fileUrl = encodeURI(
  url + this.pageUrl + "?file&line_id=" + this.attachments[index].line_id
); // attachments.php?file&line_id=1

const downloadPath =
  this.file.externalRootDirectory +
  "download/" +
  this.attachments[index].filename;
const getAttachmentCall = this.http.get(fileUrl, "", {
  "Content-Type": "blob"
});

from(getAttachmentCall)
  .pipe(finalize(() => {}))
  .subscribe(
    attachmentCallBack => {
      let b: any = new Blob([attachmentCallBack.data], { type: mimeType });

      console.log(attachmentCallBack);
      this.file
        .writeFile(
          this.file.externalRootDirectory + "download",
          "test." + fileExt,
          b,
          { replace: true }
        )
        .then(data => {
          this.fileOpener
            .open(data.nativeURL, mimeType)
            .then(() => {
              console.log("done");
            })
            .catch(error => {
              console.log(error);
              this.globalsService.dismissLoading();
            });
        });
    },
    error => {
      console.log(error);
      this.globalsService.dismissLoading();
    }
  );

但是下载的文件如果是图像,则不显示任何内容;如果是PDF,则不打开。不过,它的确显示了下载文件夹中的文件大小。

这是我的后端:

if (file_exists($server_file))
{
    $filesize = filesize($server_file);

    header('Content-Type: '.mime_content_type($server_file));
    header('Content-Disposition: attachment; filename="'.$filename.'"');
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: '.sprintf('%d', $filesize));
    header('Expires: 0');

    while (ob_get_level())
    {
        ob_end_clean();
    }

    @readfile($server_file);
}

我不确定我缺少什么。它达到了file_exists条件,并返回了readFile。

0 个答案:

没有答案