在同一浏览器的新标签页中阅读PDF文件 [英] Read PDF file in a new tab of same browser
问题描述
在HTML表格中,我显示了包含pdf文件的数据列表。所有的pdf文件名都是超链接。当用户点击超链接时,PDF应在新选项卡中打开。基本上点击超链接我正在调用一个JS函数,它调用一个方法到服务器端。在服务器端脚本中,我写了下面的代码。
In a HTML table I am showing a list of data containing pdf files. All the pdf file name are hyperlinks. When user clicks on that hyper link the pdf should open in a new tab. Basically on click of the hyperlink I am calling an JS function, which calls an method to the server side. In the server side scripting I have written the below code.
wrapper = FileWrapper(file(file_name))
response = HttpResponse(wrapper, mimetype='application/pdf; charset=utf-8')
response['Content-Disposition'] = 'inline; filename=' + file
response['Content-Length'] = os.path.getsize(file_name)
return response
此代码正常工作,点击文件打开的链接,但我想在新选项卡中打开它,而不是相同的选项卡。我已经使用 target = _blank 检查,但没有运气。有没有办法通过 content-选项或任何其他解决方案。
This code is working fine and on click of the link the file opens, but I want to open it in a new tab instead of the same tab. I have already checked with using target=_blank, but no luck. Is there any way we can do through content-"options" or any other solution.
我正在使用 Django 作为我的服务器端脚本。
I am using Django as my server side scripting.
推荐答案
您可以从JavaScript中打开一个新的标签/窗口,并在其中加载结果:
You can open a new tab/window from your JavaScript and load the results in there:
function openPDF(url){
var w=window.open(url, '_blank');
w.focus();
}
<div onclick="openPDF('pdf/1.pdf');">PDF 1</div>
<div onclick="openPDF('pdf/2.pdf');">PDF 2</div>
这篇关于在同一浏览器的新标签页中阅读PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!