最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Read PDF file in a new tab of same browser - Stack Overflow

programmeradmin3浏览0评论

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

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.

I am using Django as my server side scripting.

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

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.

I am using Django as my server side scripting.

Share Improve this question asked Feb 3, 2014 at 10:04 sandeepsandeep 3,34511 gold badges38 silver badges57 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

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>

You also can add a class on your links instead of using an inline event.

It look like this, in jQuery :

$('a.extLink').click(function(){
    window.open(this.href);
    return false;
});
发布评论

评论列表(0)

  1. 暂无评论