I have a PHP function which downloads differents files after clicking on a button (If we click on PDF button, it will load a pdf file, if we click on DOC button, it will load a doc file). It is the same function for both buttons.
My problem is when I download a file. If it's the PDF, IE will open an other page, and will close it and give me the choice to download the file, but if it's the DOC, IE will open an other page, and not close it.
The code is (for me) the same, I don't see any differences.
<pre>
<code>
public function lettrecadrageAction() {
$nom = $_POST['type'];
switch ($nom):
case 'Fichier DOC' :
$path = "ddl/lettre_de_cadrage.doc";
header('Content-Description: File Transfer');
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=lettre_de_cadrage.doc");
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($path));
ob_clean();
flush();
readfile($path);
break;
case 'Fichier PDF' :
$path = "ddl/lettre_de_cadrage.pdf";
header('Content-Description: File Transfer');
header('Content-Type: application/force-download');
header("Content-Disposition: attachment; filename=lettre_de_cadrage.pdf");
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($path));
ob_clean();
flush();
readfile($path);
break;
endswitch;
exit;
}
</code>
</pre>
The Js action for click
<pre>
<code>
$('.ldc_dl').click(function() {
var f = document.createElement("form");
f.method = "POST";
f.name = "form";
f.target = "_blank";
f.setAttribute('style', 'display: none;');
f.action = "http://" + document.domain + "/Exploitation/lettrecadrage/";
var fHtml = document.createElement("input");
fHtml.type = "text";
fHtml.name = "type";
fHtml.value = $(this).html();
console.log(fHtml);
var fSubmit = document.createElement("input");
fSubmit.type = "submit";
f.appendChild(fHtml);
f.appendChild(fSubmit);
document.body.appendChild(f);
f.submit();
document.body.removeChild(f);
return true;
}) </code>
</pre>
The HTML code for the buttons
<pre>
<code>
<div class="tab-pane fade" id="ldc">
<p>La lettre de cadrage est disponible en <button id="ldc_dl_doc" class="btn btn-link ldc_dl" type="button">Fichier DOC</button></p>
<p> Ou en <button id="ldc_dl_pdf" class="btn btn-link ldc_dl" type="button">Fichier PDF</button></p>
</div>
</code>
</pre>
(The buttons are 'Fichier PDF' and 'Fichier DOC')
Edit - Solution
With the help of jbl in comments, I resolved my problem, using an iframe :
var frame = document.createElement("iframe");
frame.setAttribute("src", "_blank");
and modify my form target
f.target = frame;
Content-Transfer-Encoding header is used in MIME email messages. It has no any sense to send it over HTTP.
Content-Disposition: attachment; header says the browser to save it rather then open in the browser. So instead of Content-Type: application/force-download i would suggest to use proper content-type for PDF and DOC files:
Content-Type: application/pdf
Content-Type: application/vnd.ms-word
With the help of jbl in comments, I resolved my problem, using an iframe :
var frame = document.createElement("iframe");
frame.setAttribute("src", "_blank");
and modify my form target
f.target = frame;
Related
When I click on the file, I can view it in browser. When I click download, it says "failed to load the pdf document".
$file = "uploads/14204-2-002.pdf";
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $file);
header('Pragma: public');
header('Expires: 0');
header('Content-Type: $mime');
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename="'.basename($file).'"'));
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Length' . filesize($file));
ob_clean();
flush();
readfile($file);
Try this one(HTML5 and no Internet explorer).
In your HTML code you can use this line:
<a href="/images/myw3schoolsimage.jpg" download="w3logo">
Specify a value for the download attribute, which will be the new filename of the downloaded file ("w3logo.jpg" instead of "myw3schoolsimage.jpg")
Moreover you can read this post of this site:
Download files from server php
I am simply trying to open a .pdf in another tab.
I start by using jQuery to post a couple of variables:
$('#resultsTable').on('click', 'tr > td > .view-pdf', function()
{
var $dataTable = $('#resultsTable').DataTable();
var tr = $(this).closest('tr');
var rowBookingNum = $dataTable.row(tr).data().JOB_REFERENCE;
var rowPartnerNum = $dataTable.row(tr).data().SHIPPER_CODE;
$.redirect('process/viewpdf.php', {'bookingnum':rowBookingNum, 'partnernum':rowPartnerNum });
});
The PHP script looks like this:
<?php
if(isset($_POST["bookingnum"]))
{
$bookingnum = $_POST["bookingnum"];
$partnernum = $_POST["partnernum"];
$dir = "D:/CargoDocsPDFs/" . $partnernum;
$file = $dir . "/" . $bookingnum . '.pdf';
if (file_exists($file))
{
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
}
?>
Using all of the above, I can get the .pdf to open in the same window. This causes the user to have to click the BACK button to go back to the previous screen.
I need the .pdf to open in a new tab. How can I adjust my code to make this happen?
You need to specify a target in $.redirect(url, [values, [method, [target, [traditional, [redirectTop]]]]])
See usage: http://github.com/mgalante/jquery.redirect
Desired usage:
$.redirect('process/viewpdf.php', {'bookingnum':rowBookingNum, 'partnernum':rowPartnerNum }, 'POST', '_blank');
I currently user html2canvas to get a div as an image.
html2canvas([document.getElementById("board")],{
onrendered: function( canvas ) {
var img = canvas.toDataURL();
var request = $.ajax({
type:"POST",
url:"downloadPic.php",
data:{downloadPic : img}
});
Then, I want to download that image (to the browser default download directory)
if(isset($_POST['downloadPic'])){
$pic = $_POST['downloadPic'];
$filteredData=substr($pic, strpos($pic, ",")+1); //html2canvas adds this
$unencodeData=base64_decode($filteredData);
$filename = "downloadedPic";
header('Content-Description: File Transfer');
header('Content-Type: image/png');
header('Content-Disposition: attachment; filename="'.basename($filename).'"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($unencodeData));
ob_clean();
flush();
readfile($unencodeData);
}
But this doesn't seem to work. If I do a file_put_contents("downloadedPic", $unencodedData) the picture is saved on my server successfully.
Do I need the header's config differently?
I have managed to use the below snippet of code, to open a pdf in a browser.Instead of opening in the same page, I would like it to open in a new browser tab.
I am not using an tag. This piece of code invokes a number of actions and at the end it is supposed to display the pdf. It works fine, but i would like it to open in a new tab.
Is this possible? and if so could you please explain to me how to do so.
Im using a Magento (EE 12.02) application and its on php 5.3.
$file = $pdf_file_path;
$filename = $file_name;
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');
#readfile($file);
You can't do that from that request.
But you can open the link in a new browser window by adding target="_blank" to your a tag, then browsers usually will use a new tab.
You can open a new tab while opening your pdf document. Example: if the code that opens your pdf document is on a page called pdfdocument.php,
that is some code like so
$pdf = file_get_contents($PDFfilename);
header('Content-Type: application/pdf');
header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
header('Pragma: public');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header('Content-Length: '.strlen($pdf));
header('Content-Disposition: inline; filename="'.basename($PDFfilename).'";');
ob_clean();
flush();
echo $pdf;
and you have a link to the page such as http://www.example.com/pdfdocument.php, you can display it in a new tab like so click here to show pdf preview
to open it in other browser tab, you should do it in the html that reference it: <a href="..." target="_blank">Download pdf
Just add exit() at the end:
$file = $pdf_file_path;
$filename = $file_name;
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');
#readfile($file);
exit();
this should just trigger the download of the file.
To open it in a new tab if you are using <a> tag you can append a target attribute like this:
<a href="your_pdf_generator_link" target="_blank" > TEXT </a>
If you have a button you can apply an inline javascript such as
<input type="button" onclick="window.open('your_pdf_generator_link','_blank')" />
Hope this helps..
I've seen this example on the documentation for PHP readfile
<?php
$file = 'monkey.gif';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
?>
How can you make it so It download multiple files say monkey.gif and girraffe.jpg
Preferably without ZIP files...
You can't. It's not a PHP limitation, it's an HTTP/Web-Browser limitation. HTTP doesn't provide a mechanism for sending multiple files over one request.
You could, however, have some PHP script that generates multiple iframes, which would initiate one download each, and fake it that way.
the whole method seems a bit pointless as a physical file actually exists on the server. just use JavaScript to open all the file urls, if you have set the header correctly in your .htaccess file then the files will just download.
I would do something like this
<script>
var files = ['filename1.jpg', 'filename2.jpg'];
for (var i = files.length - 1; i >= 0; i--) {
var a = document.createElement("a");
a.target = "_blank";
a.download = "download";
a.href = 'http://www.example.com/path_to/images/' + files[i];
a.click();
};
</script>