Downloading .xls throws error - File is not in specified format - javascript

As the title specifies, I get a File is not in specified format error when downloading the file. The following is my code. Also, the downloaded file does not have any cells.
$content = ob_end_flush();
$filename = trim($filename);
$filename = str_replace(array(' ', '/', '\\'), '', $title);
$filename .= "_Export.xls";
header('Content-type: application/vnd.ms-excel; charset=utf-8');
//header('Content-type: application/ms-excel');
header('Content-Disposition: attachment; filename='.$filename);
echo $content;

put:
ob_clean();
ob_start();
before
header('Content-type: application/vnd.ms-excel; charset=utf-8');
and try...
OR
change
header('Content-type: application/vnd.ms-excel; charset=utf-8');
to
header('Content-type: application/msexcel; charset=utf-8');
or
header('Content-type: application/octet-stream; charset=utf-8');

Related

Convert html file to pdf file in php

I have a html file that has to be converted into pdf. My code is:
<?php
include("connection.php");
extract($_REQUEST);
$query=mysql_query("select fad_html_name from fad_record where t_id='$word'") or die(mysql_error());
while($result=mysql_fetch_array($query,MYSQL_NUM))
{
extract($result);
$zipname = 'file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($result as $file) {
$zip->addFile($file);
}
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);
}
?>
This isn't working.zip file is being created, but pdf doesn't get downloaded.

angular POST not working as expected (PHP)

I am using angular to make a POST request on click using the following code in my controller...
var request = $http({
method: "post",
url: "../submit.php",
data: {
templateData: $scope.template
},
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
});
I send the data to a php file called submit.php and everything works fine, submit.php receives the data. Next thing I do with the data is write it to a file...
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$contentFile = fopen("file.txt", "w");
fwrite($contentFile, $template);
fclose($contentFile);
This seems to work, I get not errors. But now, the next thing I want to happen is to download the file to the browser. This code should work but it does not download to the browser for some reason...
header('Pragma: anytextexeptno-cache', true);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false);
header("Content-Type: text/plain");
header("Content-Disposition: attachment; filename=\"file.txt\"");
FULL CODE
<?php
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$template = "";
foreach ($request as $data) {
foreach ($data as $sub) {
for ($i = 0; $i < count($sub); $i++) {
$template .= $sub[$i];
}
}
}
$contentFile = fopen("file.txt", "w");
fwrite($contentFile, $template);
fclose($contentFile);
header('Pragma: anytextexeptno-cache', true);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false);
header("Content-Type: text/plain");
header("Content-Disposition: attachment; filename=\"file.txt\"");
?>
The only thing you missed is to actually output your "file".
<?php
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$template = "";
foreach ($request as $data) {
foreach ($data as $sub) {
for ($i = 0; $i < count($sub); $i++) {
$template .= $sub[$i];
}
}
}
header('Pragma: anytextexeptno-cache', true);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false);
header("Content-Type: text/plain");
header("Content-Disposition: attachment; filename=\"file.txt\"");
// here's the thing:
echo $template;
?>
This only works for text/plain.
You don't need to save the content to a file first.
NOTE
You can never enforce a browser to "download" a file. Some browsers might decide to display that file. The "stranger" the file is, the more likely the user will be prompted to download.
<?php
$postdata = file_get_conteenter code herents("php://input");
$request = json_decode($postdata);
$template = "";
foreach ($request as $data) {
foreach ($data as $sub) {
for ($i = 0; $i < count($sub); $i++) {
$template .= $sub[$i];
}
}
}
$contentFile = fopen("file.txt", "w");
fwrite($contentFile, $template);
fclose($contentFile);
/************
Return full path of text file in json
***********/
?>
and in ajax success you can open that file in new window using window.location function.

Download Pdf with php, Not working in explorer and safari

I've got a script to download pdf after submitting a form to db works fine on chrome and firefox but when i download my pdf on safari or explorer my pdf is empty and cannot be opened, i have tried many things, right now im using javascript whit window.location and it works on safari and explorer but i would really like my php script to work on all browsers
<?php
ob_start();
if ($_REQUEST["action"] == "download") {
$file = base64_decode($_REQUEST["download"]);
//Insert to download table....
if (isset($_SERVER['HTTP_USER_AGENT'])) {
$agent = $_SERVER['HTTP_USER_AGENT'];
}
if (strlen(strstr($agent, 'Firefox')) > 0) {
$browser = 'firefox';
} else if (strlen(strstr($agent, 'Chrome')) > 0) {
$browser = 'chrome';
} else {
$browser = 'safari';
}
if ($browser == 'safari'){
?>
<script type="text/javascript">
window.location = '<?php echo $file;?>';
</script>
<?php
} else { ?>
<?php
if ($file) {
header('Pragma: public'); // required
header('Expires: 0'); // no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false);
header('Content-Type: application/octet-stream');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($file)) . ' GMT');
header('Content-Disposition: attachment; filename='.basename($file));
header("Content-Transfer-Encoding: binary");
header('Content-Length: ' . filesize($file)); // provide file size
header('Connection: close');
readfile($file);
exit();
}
}
}
I solved the problem by removing some of the headers, now im only using this
<?php
ob_start();
if ($_REQUEST["action"] == "download") {
$file = base64_decode($_REQUEST["download"]);
if ($file) {
header('Pragma: public'); // required
header('Expires: 0'); // no cache
header("Content-disposition: attachment; filename=".basename($file));
header("Content-type: application/pdf");
readfile($file);
exit();
}
}
you should use the function ob_flush(); if you want to publish this kind of document in explorer

How to live preview a PDF file which is saved as Longblob data in MySQL Server using javascript?

How to make live preview a PDF file in my local website ( no internet ) which is the PDF saved in Longblob? I use this script to download the PDF :
<?php
if(isset($_GET['id']))
{
include "../conn.php";
$query = "select * from file where id = '".$_GET['id']."'";
$result = mysql_query($query) or die (mysql_error);
$download = mysql_fetch_array($result);
$name = str_replace('%20', ' ',$download['file_name']);
$type = $download['file_type'];
$size = $download['file_size'];
$content = $download['file'];
header("Content-disposition: attachment; filename=\"".$name."\"");
header("Content-length: ".$size."");
header("Content-type: $type");
echo $content;
exit;
}
?>
I have use almost all open source PDF Viewer plugins like from http://viewerjs.org , but failed to display the wanted PDF file. Need your help guys..
you shoud have a pdf plugin and you should use code looks like :
header('Content-type: application/pdf');
header("Cache-Control: no-cache");
header("Pragma: no-cache");
header("Content-Disposition: inline;filename='document.pdf'");
header("Content-length: ".strlen($binary_contents_from_database));

downloading .xls file doesn't gives data within file in php

I want data into .xls file...but it doesn't give....
$this->load->view("partial/footer_excel");
$content = ob_end_flush();
$filename = trim($filename);
$filename = str_replace(array(' ', '/', '\\'), '', $title);
$filename .= "_Export.xls";
ob_clean();
ob_start();
header('Content-type: application/vnd.ms-excel; charset=utf-8');
//header('Content-type: application/ms-excel');
header('Content-Disposition: attachment; filename='.$filename);
echo $content;
die();

Categories