繁体   English   中英

如何打开这个文件? 或改为下载,在新窗口中显示预览?? 我有pdf档案

[英]How to open this file?? or Instead Downloading, show preview in new window?? I have pdf file

此代码直接保存文件,我想预览pdf文件注意:我的内容为纯文本格式,因此首先我必须创建pdf文件,然后打开或预览

function download(filename, text) {
 var element = document.createElement('a');
 element.setAttribute('href', 'data:text/plain;charset=utf-8,' +  encodeURIComponent(text));
 element.setAttribute('download', filename);

 element.style.display = 'none';
 document.body.appendChild(element);

 element.click();

 document.body.removeChild(element);
 }

使用对象标签嵌入pdf

var objectPDF = document.createElement("object");
objectPDF.data = 'data:application/pdf;base64,' +text;
objectPDF.type = 'application/pdf';
document.body.appendChild(objectPDF);

您也可以使用iframe

如注释中所述,如果您只想在新窗口中显示文件,请删除doanload属性并添加target="_blank" 在这里取样

function download(filename, text) {
 var element = document.createElement('a');
 element.setAttribute('href', 'data:application/pdf;charset=utf-8,' + encodeURIComponent(text));
  element.setAttribute('target','_blank');

 element.style.display = 'none';
 document.body.appendChild(element);

 element.click();

 document.body.removeChild(element);
 }

为了使其更简单,请使用window.open()而不是临时链接。 在这里取样

function viewFile(filename, text) {
  window.open('data:application/pdf;charset=utf-8,' + encodeURIComponent(text));
}

尝试这个

function download(filename, text) {
 var element = document.createElement('a');
 element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));

 element.style.display = 'none';
 document.body.appendChild(element);

 element.click();

 document.body.removeChild(element);
 }

使用embed元素保存pdf文件

var pdf = "pdf file link here";

    $('<div/>')
        .html('<embed class="fullFrame" ' +
              'type="application/pdf" ' +
              'src="' + pdf + '"></embed>')
        .dialog({
            autoOpen: true,
            modal: true,
            height: 400,
            width: 400,
            title: "Resume"
        });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM