Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在iframe中显示数据库中的pdf_Javascript_Php_Jquery_Html_Mysql - Fatal编程技术网

Javascript 如何在iframe中显示数据库中的pdf

Javascript 如何在iframe中显示数据库中的pdf,javascript,php,jquery,html,mysql,Javascript,Php,Jquery,Html,Mysql,我制作了一个文件上传系统,上传的文件存储在数据库中。现在我已经创建了一个iframe,以便可以查看其中的文件。目前,我已将iframe设置为不在db内显示文件,但无法将iframe设置为显示db中的文件。我对此做了很多研究,但遇到了一个又一个的障碍,所以我真的希望你能在这方面提供帮助,非常感谢! 下载.php <?php function output_file($file, $name, $mime_type='') { //if(!is_readable($file)) di

我制作了一个文件上传系统,上传的文件存储在数据库中。现在我已经创建了一个iframe,以便可以查看其中的文件。目前,我已将iframe设置为不在db内显示文件,但无法将iframe设置为显示db中的文件。我对此做了很多研究,但遇到了一个又一个的障碍,所以我真的希望你能在这方面提供帮助,非常感谢!

下载.php

    <?php
function output_file($file, $name, $mime_type='')
{
//if(!is_readable($file)) die('<script type="text/javascript">alert("File not found!");location.replace("index.php")
    if(!is_readable($file)) die('<script type="text/javascript">alert("File not found!");location.replace("par_upload.php")
    </script>');

$size = filesize($file);
$name = rawurldecode($name);
$known_mime_types=array(
"pdf" => "application/pdf",
"txt" => "text/plain",
"html" => "text/html",
"htm" => "text/html",
"exe" => "application/octet-stream",
"zip" => "application/zip",
"doc" => "application/msword",
"xls" => "application/vnd.ms-excel",
"ppt" => "application/vnd.ms-powerpoint",
"gif" => "image/gif",
"png" => "image/png",
"jpeg"=> "image/jpg",
"jpg" => "image/jpg",
"php" => "text/plain"
);
if($mime_type==''){
$file_extension = strtolower(substr(strrchr($file,"."),1));
if(array_key_exists($file_extension, $known_mime_types)){
$mime_type=$known_mime_types[$file_extension];
} else {
$mime_type="application/force-download";
};
};

@ob_end_clean();


if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');



   // header('Content-Type: ' . $mime_type);
    header('Content-type: application/pdf');

header('Content-Disposition: inline; filename="'.$name.'"');
//header('Content-Disposition: attachment; filename="'.$name.'"');


header("Content-Transfer-Encoding: binary");
header('Accept-Ranges: bytes');
header("Cache-control: private");
header('Pragma: private');
header("Expires: Mon, 26 Jul 2017 05:00:00 GMT");
if(isset($_SERVER['HTTP_RANGE']))
{
list($a, $range) = explode("=",$_SERVER['HTTP_RANGE'],2);
list($range) = explode(",",$range,2);
list($range, $range_end) = explode("-", $range);
$range=intval($range);
if(!$range_end) {
$range_end=$size-1;
} else {
$range_end=intval($range_end);
}
$new_length = $range_end-$range+1;
header("HTTP/1.1 206 Partial Content");
header("Content-Length: $new_length");
header("Content-Range: bytes $range-$range_end/$size");
} else {
$new_length=$size;
header("Content-Length: ".$size);
}
$chunksize = 1*(1024*1024);
$bytes_send = 0;
if ($file = fopen($file, 'r'))
{
if(isset($_SERVER['HTTP_RANGE']))
fseek($file, $range);

while(!feof($file) &&
(!connection_aborted()) &&
($bytes_send<$new_length)
)
{
$buffer = fread($file, $chunksize);
print($buffer);
flush();
$bytes_send += strlen($buffer);
}
fclose($file);
} else

die('Error - cannot open file.');
die();
}
set_time_limit(0);
$file_path='par_files/'.$_REQUEST['filename'];
output_file($file_path, ''.$_REQUEST['filename'].'', 'text/plain');
?>

尝试从src路径生成一个完整的URL,并且不要在路径中包含任何空格。比如说:

omyFrame.src = "http://example.com/pdfs/PRICE%20LIST.pdf";
<a href="javascript:void(0)"
  onclick="openPdf('download.php?filename=<?php echo $name;?>')"
  title="click to download">VIEW <span class="glyphicon glyphicon-paperclip"
  style="font-size:20px; color:blue"></span>
</a>
function openPdf(src)
{
  var omyFrame = document.getElementById("myFrame");
  omyFrame.style.display="block";
  omyFrame.src = src;
}
我一直在用这种方式操纵iframe,它似乎很管用

我知道你说你是从数据库中获取这些PDF文件的,你不是想加载一个特定的文件,但是为了使用iframe,最好是你的服务器从一个特定的URL提供每个PDF文件的内容

好的,那么,根据您下面的评论,您可以使用如下链接:

omyFrame.src = "http://example.com/pdfs/PRICE%20LIST.pdf";
<a href="javascript:void(0)"
  onclick="openPdf('download.php?filename=<?php echo $name;?>')"
  title="click to download">VIEW <span class="glyphicon glyphicon-paperclip"
  style="font-size:20px; color:blue"></span>
</a>
function openPdf(src)
{
  var omyFrame = document.getElementById("myFrame");
  omyFrame.style.display="block";
  omyFrame.src = src;
}

这个javascript在哪里,在HTML之前或之后,可能在创建元素之前运行。即。ready@ArtisticPhoenix我没有在这里包含完整的代码好吧,那很好,但是你显然没有足够的基本内容,否则我就不会问了。调试时,我们必须考虑它可能失败的方式。一种方法是,当JavaScript运行时,如果DOM还没有准备好,那么文件是如何存储的?数据库中的二进制数据或只是指向文件位置的URL?myframe.src=PRICE LIST.pdf;建议它是一个文件,如果它是我的话,我会避开空格,因为在某些情况下可能会导致问题。这不是我想要的检查imgSee我上面的扩展响应。我仍然在谈论从DB获取PDF,只是假设您可以从特定URL提供这些PDF。如果您有一个URL可以用于在新网页中打开其中一个PDF,那么您应该能够将iframe的src设置为相同的URL。如果您尝试这样做会发生什么?如果您能够在使用原始链接之前在单独的窗口中打开所有PDF,那么您现在必须非常接近能够在iframe中打开它们,因为这只是获得每个新窗口打开时填充到iframe的src属性中的相同URL的问题。很遗憾,远程调试这样的东西太难了。很高兴听到。耶!