Easiest Way to Open a Download Window Without Navigating Away from the Page

Easiest way to open a download window without navigating away from the page

7 years have passed and
I don't know whether it works for IE6 or not, but this prompts OpenFileDialog in FF and Chrome.

var file_path = 'host/path/file.ext';
var a = document.createElement('A');
a.href = file_path;
a.download = file_path.substr(file_path.lastIndexOf('/') + 1);
document.body.appendChild(a);
a.click();
document.body.removeChild(a);

How do you serve a file without leaving the page?

To avoid leaving the page (if you do this the page tries to close itself first, so that it's sure that you've saved everything, and you get warning messages if you haven't) or leaving blank tabs (which I don't like, nor the use of the depreciated target attribute) I've used an iframe, whose src attribute is changed in javascript.

This works everywhere except some versions of Opera, which I have considered an acceptable loss (I might fix that via the use of one of the other solutions plus browser detection later).

A download script and statcounter working together without opening a new page

What you want is not possible. (executing javascript on pdf download) Statcounter is an script and is executed by the browser by being included on the html. If you download a pdf file, you are not executing any js.

However, statcounter can see what links are pressed and therefore you can find which files were downloaded; as long your files are downloaded by using regular links on an html that has statcounter included. You don't need to do anything at all, they would be counted by default.

The idea Dachi gave, is to add an sql insertion to that php code of yours.

<?php
$dbh = new PDO("sqlite:/path/to/database.sdb");
//Put the insertion code here.
//Insert things like the IP, the login name, the time, etc.
header('Content-disposition: attachment; filename=brochure-company-details.pdf');
header('Content-type: pdf');
readfile('brochure-company-details.pdf');

That works too, and is a good choice if you are allowing downloads by not using regular links or not from an html that has statcounter.



Related Topics



Leave a reply



Submit