download attribute of html is not working for iphone browser - javascript

I am trying to force download pdf instead of opening.
I have used
<a href='path' download>
but its still open file in mobile devices like iphone
I need to force to download file instead of open
Thanks in advance

Im giving you a idea for force downloading through php,
Create a PHP file download.php, change accordingly to your php app.
<?php
// Ajmal Praveen
$file_name = 'file.pdf';
$file_url = 'http://yoururl.com/' . $file_name;
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"".$file_name."\"");
readfile($file_url);
exit;
?>
Make a link
<a href="./download.php?file=file.pdf" download>Download</a>

Related

Read pdf without download and print it with html php

Good evening,
I would like to create a feature for my users that allows them to read a pdf file without downloading or printing it.
They will only be able to read the pdf file.
I tried pdf.js which did not work,
I also tried to convert my pdf to html via a script,
I also tried this php code but the download and print button appears:
<?php
// The location of the PDF file
// on the server
$filename = "/path/to/the/file.pdf";
// Header content type
header("Content-type: application/pdf");
header("Content-Length: " . filesize($filename));
// Send the file to the browser.
readfile($filename);
?>
Do you have any solutions or technologies to suggest to me,
I use PHP, HTML and JS.
Try this one:
<?php
// Store the file name into variable
$file = '/path/to/the/filename.pdf';
$filename = 'filename.pdf';
// Header content type
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
// Read the file
#readfile($file);
?>
PHP uses a standard code to display the pdf file in web browser. The process of displaying pdf involves location of the PDF file on the server and it uses various types of headers to define content composition in form of type, Disposition, Transfer-Encoding etc. PHP passes the PDF files to read it on the browser. Browser either shows it or download it from localhost server then display pdf.
Note: PHP is not actually reading the PDF file. It does not recognize File as pdf. It only passes the PDF file to the browser to be read there. If copy the pdf file inside htdocs folder of XAMPP then it does not need to specify the file path.
The php script you offered only can download the file. if you use php ,you can try the demo ,which is using pdf2text here
or try the method in this link read-pdf-files-with-php

Force IE11 to save download CSV file

I am trying to download a CSV file to the browser.
I am unable to get it to properly download the file in IE11. Instead it opens a new tab and write the CSV contents to the tab.
I am using just the following javascript:
window.open(sUrl, '_target');
In the IE Developer tools, I can see the Response Header shows:
Content-Type: text/csv; charset=UTF-8
I would like it to prompt the user to save the file.
I've changed the browser settings to always download, yet it still doesn't do it.
I would like to fix this from a change to the Javascript, if possible.
How can I do that?
As far as I know, it's not possible to do this with JavaScript. Take a look at this answer if you're comforable using PHP to download the file.
According to that answer, your PHP should look something like this:
<?php
$file_url = 'http://www.myremoteserver.com/file.csv';
header('Content-Type: text/csv; charset=UTF-8');
header("Content-Transfer-Encoding: Quoted-Printable");
header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\"");
readfile($file_url);
?>

Download file from URL, then save to user computer via dialog

How can I download file (I just know the URL - the file can have x MB) and then save via dialog to user computer? (Dialog = user can choose the directory, where he want to save the file). Just like in screen.
I have absolutely no idea how to do it. I Google it and I found only how to load the file via FILE input, but I do not need that.
File suffix is "*.db", thanks guys, I'm not such a web guy.
You can do that pretty easily.
Make a php file, let's call it index.php. And this should be all the code you need:
<?php
$file = "/your/location/file.db";
header("Content-Type: application/octet-stream");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename='" . basename($file) . "'");
readfile ($file);
?>
I hope I've understood what you actually need. This should do the trick.

PHP CSV create then echo javascript redirect

I am creating a CSV file in PHP, no biggie there, and then force downloading it for the user, again no biggie there. The problem I am coming across is the redirect portion. I know that you cannot create a file and push to the user and then use php redirect afterwards, as it kills the CSV creation due to the HTTP protocols. So instead, I am doing a javascript redirect as follows:
echo '<script>';
echo ' window.location = "'.base_url().'link/'.$item_id.'"';
echo '</script>';
die;
This works fine (it reloads the page), but the portion above that is echoed gets added on to the CSV file when it is downloaded. Here is the php code that creates the CSV and force downloads it:
fwrite($csv, $csv_data);
fclose($csv);
header('Content-type: application/vnd.ms-excel');
header('Content-Length: ' . strlen($csv_data));
header("Content-disposition: attachment; filename=$file_name");
readfile($file_path);
What am I missing in terms of not having the <script> text getting appended to the CSV?
Thanks in advance.

Automatic "download" PDF as upon form submission

I have a form button, when clicked it submits the form.
I'd like that at the same time, the browser starts downloading a PDF file. I wonder how can I do this? Please keep in mind that PDF are usually opened by the browser by default, but I'd like the browser to "Save As" the file and not open the file. Is this do-able using html only or do I need javascript?
If you are using PHP in the server side try this. This is tested code which is running in one of my websites.
<?php
ob_start();
$file = 'YOUR-FILENAME-HERE.pdf';
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');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit();
}
?>
Browser will prompt to download without displaying it in the browser.
You can use this plugin to download the files using javascript.
Otherwise in javascript, you can write code for it.
$('a').click(function(e) {
e.preventDefault(); //stop the browser from following
window.location.href = 'downloads/file.pdf';
});
Download now!
Or, you can use this .
In PHP :
<?php
header('Content-type: application/pdf');
header('Content-disposition: attachment; filename=filename.pdf');
readfile("file.pdf");
?>
In Javascript :
<body>
<script>
function downloadme(x){
winObj = window.open(x,'','left=10000,screenX=10000');
winObj.document.execCommand('SaveAs','null','download.pdf');
winObj.close();
}
</script>
<a href=javascript:downloadme('file.pdf');>Download this pdf</a>
</body>
In that case, you'd have to configure your webserver to force a PDF to Download.
If you're using Apache, you might as well take a look at this article ->
http://www.thingy-ma-jig.co.uk/comment/7264
Or, if you don't have to support all browsers, you could get away with just using the html5 download attribute -> http://updates.html5rocks.com/2011/08/Downloading-resources-in-HTML5-a-download
Option 1: You can let the url, which your form submits to, return the download.
Option 2: Use an ajax call to submit the data onclick, set the window.location to your download url.
Whether the pdf is opened in the browser or downloaded, is a client side setting which you cannot control.

Categories

Resources