首页 > 解决方案 > 即在 iframe 中显示 base 64 编码文件

问题描述

我有一个 base 64 字符串和文件 mime 类型和文件名。该文件可以是各种类型,例如图像、pdf、office 文件等。我的 html 中有一个 iframe 来显示 base 64 内容。它在 chrome 中运行良好,并显示 pdf、图像、文本文件和下载 docx(我也宁愿在 iframe 中显示)我使用的代码:

viewFile(cont, ftype, fname) {

    let mime = mimeTypes.getType(fname);

 
        var downloadLink = document.createElement('a');
        downloadLink.href = typeof cont === 'string' 
            ? this.createFileStreamForConverting(cont, mime, fname)
            : window.URL.createObjectURL(cont);
            
        if(mime == 'application/pdf')
        {
        const blobUrl = window.URL.createObjectURL(this.b64toBlob(cont, mime));
        downloadLink.href =  blobUrl;
        }
        
        downloadLink.style.display = 'none';
        downloadLink.target = 'origFileViewFrame';
        document.body.appendChild(downloadLink);
        downloadLink.type = mime;
        if(this.isDownloadType(ftype))
          downloadLink.download = fname;
        downloadLink.click();


    }

我试图在 ie 上运行代码,但无法显示带有错误的项目:传递给系统调用的数据区域太小。所以我搜索并看到 ie 有一个问题,可以使用 msSaveBlob 解决

if (navigator && navigator.msSaveBlob) {
    navigator.msSaveBlob(cont, fname);
}

但我不想下载文件。还有其他方法吗?另外,我希望文件显示在 iframe 中,而不是在空白窗口中。有什么事吗?

标签: internet-exploreriframe

解决方案


推荐阅读