Php 使用jqueryajax显示pdf blob
这是我的jQuery: $(文档).ready(函数(){ var instrID; var-catPhp 使用jqueryajax显示pdf blob,php,jquery,ajax,pdf,Php,Jquery,Ajax,Pdf,这是我的jQuery: $(文档).ready(函数(){ var instrID; var-cat $(window).load(function(){ }); $.post('ajax.php', {InstrumentID: instrID, catView: "pdf"}, function(data){ $('#displayPDF').append("<php> header('Content-type
$(window).load(function(){
});
$.post('ajax.php', {InstrumentID: instrID, catView: "pdf"}, function(data){
$('#displayPDF').append("<php> header('Content-type: application/pdf') </php>");
$('#displayPDF').append("<php> echo("+ data +") </php>");
});
$(窗口).load(函数(){
});
$.post('ajax.php',{InstrumentID:instrID,catView:pdf},函数(数据){
$('#displayPDF')。追加(“标题('Content-type:application/pdf');
$('#displayPDF')。追加(“echo(“+data+”));
});
这是我的ajax或ajax.php:
<?php
include '../include/xxxxx.php';
$instrumentID = $_POST['InstrumentID'];
$category = $_POST['catView'];
$sql = "SELECT * FROM `xxxxx` WHERE `InstrumentID` = '" . $_POST['InstrumentID'] . "'";
$results = mysql_query($sql);
if($category == "pdf")
{
header("Content-type: application/pdf");
echo (mysql_result($results, 0, 'Instrument'));
}
?>
我认为您不能以这种方式内联显示PDF。请尝试切换到iframe-这应该可以。这是将iframe的位置设置为您的ajax.php。您没有为instrID
设置值,也没有对输入进行清理
无论如何,您可以将pdf嵌入页面,而不是使用ajax
var source = 'ajax.php?InstrumentID='+encodeUriComponent(instrID)+'&catView=pdf';
$('#displayPDF').append('<object data="'+source+'" type="application/pdf">'+
'<embed src="'+source+'" type="application/pdf"/></object>');
var source='ajax.php?InstrumentID='+encodeUriComponent(instrID)+'&catView=pdf';
$('#displayPDF')。附加(''+
'');
然后使用$\u GET
而不是在php中发布。也许这不是对特定问题的回答,但是,这个问题在第一次谷歌搜索时就出现了,所以我想分享一下当pdf是blob
数据时下载pdf的方法
$.ajax({
url: 'someurl',
method: 'get',
data: { param1: value1, param2: value2 },
xhr: function() {
const xhr = new XMLHttpRequest();
xhr.responseType= 'blob'
return xhr;
},
success: function (blob) {
const link=document.createElement('a');
link.href=window.URL.createObjectURL(blob);
link.download="my-pdf-file-name";
link.click();
}
});
两者:$instrumentID=$\u POST['instrumentID'];$category=$\u POST['catView'];应该是$instrumentID=$\u GET['instrumentID'];$category=$\u GET['catView'];@user908759是的,更改是从ajax post请求更改为嵌入式对象的get请求。我将post更改为get,但仍然不走运。是否有其他方法可以使用php、mysql和jquery显示pdf blob?我一直在疯狂搜索,没有找到太多帮助。不过谢谢你。感谢你的帮助,我刚刚找到了它…chec我的新答案我猜iframe会出现在我的节目中。你有例子吗?没有。我不使用iframe。谷歌上应该有很多例子。
$.ajax({
url: 'someurl',
method: 'get',
data: { param1: value1, param2: value2 },
xhr: function() {
const xhr = new XMLHttpRequest();
xhr.responseType= 'blob'
return xhr;
},
success: function (blob) {
const link=document.createElement('a');
link.href=window.URL.createObjectURL(blob);
link.download="my-pdf-file-name";
link.click();
}
});