[英]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.