Save files to disk

How would you create a button to force download a pdf file from the projects resource file. (force download file rather than display the pdf in another browser tab)

I wouldn’t use a button. I’d use a hyperlink. That way, the visitor could right-click the link and save the file if they want.

Also, the PDF could be compressed as a .zip file. That type of file would likely be downloaded instead of shown in a browser.

If you want to make sure the PDF is downloaded, then maybe try searching Google for alternate methods…

…but I don’t usually mess with that stuff for PDF downloads. I prefer using simple Hyperlinks.

I mannaged to get click to download file to disk working but can’t seem to get it to link to the pdf in the tumult hype resource file:

// insert code for mouse click here

function Download(){
window.open(‘FILENAME.PDF’);
}

// insert code for mouse click here
function SaveToDisk(fileURL, fileName) {
// for non-IE
if (!window.ActiveXObject) {
var save = document.createElement(‘a’);
save.href = fileURL;
save.target = ‘_blank’;
save.download = fileName || ‘FILENAME.PDF’;

    var event = document.createEvent('Event');
    event.initEvent('click', true, true);
    save.dispatchEvent(event);
    (window.URL || window.webkitURL).revokeObjectURL(save.href);
}

// for IE
else if ( !! window.ActiveXObject && document.execCommand)     {
    var _window = window.open(fileURL, '_blank');
    _window.document.close();
    _window.document.execCommand('SaveAs', true, fileName || fileURL)
    _window.close();
}

}

SaveToDisk(“FILENAME.PDF”);

To directly reference the PDF file, you should use:

${resourcesFolderName}/FILENAME.PDF

That will populate the ${resourcesFolderName} with the name of your document's resources folder. Or, you could use the full URL to the PDF on your server.

I would compress the file as a zip and then using (as Daniel says) the url:

${resourcesFolderName}/FILENAME.pdf.zip

or just

${resourcesFolderName}/FILENAME.zip

and placing it in a link tag or connecting it to a button with a “new function…” javascript on click as follows:

window.open("${resourcesFolderName}/FILENAME.zip", "_blank");

should enable the document to download immediately.

NOTE If you go the route of using the *download attribute then it won’t work in Safari or IE

1 Like