Home
Tuesday, April 25, 2017
12:56:51 PM
Users online: 0   You are here >> Home > Web Design

Forums | Web Design Forums search
Forum FAQ
   
  1 | 2 Next Page 
How to force download a pdf iinstead of view in browser.
moofactory 
8/5/08 5:15:09 PM
Titan


I have a task of finding a way to do this only under normal html or JSP.

its giving me the shits cause it can easily be done with PHP.. but the sites not able to use this.

Anyone know of a javascript, xml or any other kind of script that can do this without using other back end code languages? hell even action scripting in flash.

any ideas let me know.

And no, save target as isnt an option we want to use :P

-----
Quote by Ben Mansill
I've always waited for the day here when we talk about anal sex with animated Disney lions.

superfireydave 
8/5/08 5:43:24 PM
Titan

Change the header information for it I imagine.
I'll see if I can find something.

edit: *doh* that'll probably involve PHP.
Why is it such a big issue? It seems a trivial, non required issue to me and therefore I wouldn't waste my time with it.


 
<html>
<body>
<script>
function downloadme(x){
myTempWindow = window.open(x,’’,’left=10000,screenX=10000’);
myTempWindow.document.execCommand(’SaveAs’,’null’,x);
myTempWindow.close();
}
</script>

<a href=javascript:downloadme("/test.pdf");>Download this pdf</a>
</body>
</html>

Seems to be a work around. It essentially opens it, saves it, then closes the window it initially opens.

(from : http://www.faqts.com/knowledge_base/view.phtml/aid/4906 )


Again though: it's a trivial and to me it's a silly thing to worry about in the grand scheme of things.


Edited by superfireydave: 8/5/2008 5:49:19 PM

-----
Mreow?

fabricator 
8/5/08 6:26:50 PM
Overlord

superfireydave I tried that example out, doesn't work in IE 6 or Firefox. Says its IE specific, and i'll bet it doesn't work with a popup blocker either.

From the same FAQ (this trick doesn't work either I just tried it).

"After having spent some time finding a way to by-pass the file
association problem for PDF's I just renamed the PDF file !

By removing the file extention '.pdf' you cause your browser to ask
you what you want to do with the unknown file.

When saving it, it does appearently read the file and recognize it as
a PDF. This will cause the Save As dialog box to save it with PDF file extention and so you just need to hit the OK button !
"


Just bung them into a zip file seems to be the only workable option.

-----
location: adelaide AMD 3000+, Epox 8RDA6+ PRO, 1024 meg DDR400 cas 2.5, P/Color 9800SE AIW, 1xWD 120gig HDs 3xWD 320gig. lite-on DVD & cd-rw 550W PSU

superfireydave 
8/5/08 6:32:55 PM
Titan

Yeah, it's a very specific fix for a very anal retentive problem.

-----
Mreow?

.:Cyb3rGlitch:. 
8/5/08 7:08:22 PM
Titan

I know Foxit Reader makes you download it, because it can't render the PDF in the browser.

-----
Cyb3rGlitch Tutorials: http://www.cyb3rglitch.com/
Tutorials for the aspiring beginner.

moofactory 
9/5/08 4:33:50 PM
Titan

Thanks for the info guys.. were getting there.
It is a very annoying thing.

-----
Quote by Ben Mansill
I've always waited for the day here when we talk about anal sex with animated Disney lions.

superfireydave 
9/5/08 4:55:09 PM
Titan

I still don't understand the problem with viewing them in the web browser?

You'd assume that people would save them just like an image if they wanted to keep them, and if not, well then they'd just look at in the browser?
edit: What I'm saying is you're cutting out a usability feature. To me, that's a really silly mistake to make.


Edited by superfireydave: 9/5/2008 4:55:31 PM

-----
Mreow?

iamthemaxx 
10/5/08 2:52:01 PM
Mod
SuperHero

Immortal


nicephotog-jvm.net your post has been deleted, no advertising here.

-----

moofactory 
11/5/08 12:57:21 AM
Titan

Quote by superfireydave
I still don't understand the problem with viewing them in the web browser?

You'd assume that people would save them just like an image if they wanted to keep them, and if not, well then they'd just look at in the browser?
edit: What I'm saying is you're cutting out a usability feature. To me, that's a really silly mistake to make.
Edited by superfireydave: 9/5/2008 4:55:31 PM



We cant afford to assume.
Its quite simple really, The average user isnt that technologically savy. You have to factor in people that are going to this site, are those that are not all that familiar with the internet, are possibly elderly, have difficulty grasping these kinds of concepts etc.

Besides, this is just what business wants. Sometimes they make dumb requests.. But we don't argue we just deliver a solution if its possible.

At the moment, we have zips for download and normal pdf links for view.. we want to do this so we can reduce the size of the site.


Edited by moofactory: 11/5/2008 12:58:47 AM

-----
Quote by Ben Mansill
I've always waited for the day here when we talk about anal sex with animated Disney lions.

superfireydave 
11/5/08 9:39:19 AM
Titan

I'd say it's your responsibility as someone working with the web to keep a standard of usability.

And you cannot afford to assume that the user wants to download instead of just viewing. That's the major flaw. Your reasononing isn't really valid at all. If they're can't figure out how to save it to disk in a normal situation, and it auto does it perhaps they wont understand where it was saved.

I don't think it's a very well thought out idea at all. The only single thing I imagine it saves is a small amount of bandwidth from people being forced to download rather then having the option of going back to the site.

I think you're out of luck anyway if you're looking for a simple easy html or javascript implementation. You could easily do it with PHP, but that's a bit more technical.

Still, it's my opinion that it's a bad idea anyway.

-----
Mreow?

nicephotog-jvm.net 
11/5/08 6:10:30 PM
Master

For JSP, well you don't, you use a servlet.
The trouble is that even with a redirect "?=" URL information link the
browser behavior is a unpredictable for the supplier/calling page.
You supply a downloader link(servlet) and a url page caller link.
I built and use one at one of my sites, it also uses JDBC to verify
(2.3 servlet spec).


Edited by nicephotog-jvm.net: 11/5/2008 6:11:23 PM

-----
ERROR @ [ ForumReply.DLL ]
Line 983395876948
[ CyborgLoop.DLL ] not loaded
ERROR #232 (0x85100084) Fatal Exception
program C:\progra~1\nicephotoganswers\bin\nicm.exe

moofactory 
11/5/08 8:38:13 PM
Titan

Quote by superfireydave
I'd say it's your responsibility as someone working with the web to keep a standard of usability.

And you cannot afford to assume that the user wants to download instead of just viewing. That's the major flaw. Your reasononing isn't really valid at all.



I don't think you read what ive said right, We currently have an option for both view and download links for the same file.

Currently this is done by a link to pdfs for view and a seperate zip for download. this doubbles the size.

what we want to do is have the ability to view as current but instead of a zip for the download link, download the same pdf file.

So were not taking away any options. And if anything.. the user is left with one less headache of having to know how to unzip a file.

Anyway as I said. not my call thats just what business is asking for. So it has nothing to do with me Im just trying to make it happen.

-----
Quote by Ben Mansill
I've always waited for the day here when we talk about anal sex with animated Disney lions.

superfireydave 
11/5/08 9:06:21 PM
Titan

Oh right -_-

I still think it's ridicuous >_<

You might need to look into a PHP solution.

-----
Mreow?

LagrangeApex 
31/5/08 11:33:36 PM
Champion

 
<?php
if (isset($_GET['file'])) {
$file = $_GET['file']
if (file_exists($file) && is_readable($file) && preg_match('/\.pdf$/',$file)) {
header('Content-type: application/pdf');
header("Content-Disposition: attachment; filename=\"$file\"");
readfile($file);
}
} else {
header("HTTP/1.0 404 Not Found");
echo "<h1>Error 404: File Not Found: <br /><em>$file</em></h1>";
}
?>




Save this little snippet as a PHP file somewhere on your server and you can use it to make a file download in the browser, rather than display directly.
If you want to serve files other than PDF, remove or edit line 5.

You can use it like so;

Add the following link to your HTML file.
<a href="download.php?file=my_pdf_file.pdf">Download the cool PDF.</a>

Um, thats it...
The paths will need to be either relative URLS to the download.php file, or absolute URLs.
You can reuse it for multiple files, just add links to your web pages.


This *should* not serve any file that is unreadable by the webserver process, but it DOES put the power to access files on your webserver to the end user.
I've limited this script to only allow files ending with pdf to be downloaded, or else show a 404 error.
You could remove this, as well as the header content type declaration that specifies the file MIME type being sent to the browser.

You could beef up security by adding a check on the file to ensure that it is in only one location or subdirectory.

EDIT - CODE formatting.


Edited by LagrangeApex: 31/5/2008 11:34:14 PM

-----
-=-=-=-=-

Girvo 
1/6/08 2:10:04 AM
Immortal

Wicked snippet, but he said he can't use PHP :)

And I'm really weird. I so would've put all that in a class. :|

-----
Quote by Kothos
I look like a malting Yeti.



LagrangeApex 
1/6/08 7:01:18 PM
Champion

oops...got caught up in the thought experiment and just inverted the requirements. :D

Sorry, I don't know JSP.


You could put that in a class but meh, effort > reward.
Not everything needs to be OO.

-----
-=-=-=-=-

Girvo 
3/6/08 3:16:05 PM
Immortal

Lies. Everything must be OO.

-----
Quote by Kothos
I look like a malting Yeti.



nicephotog-jvm.net 
9/6/08 4:58:43 PM
Master

I second that [LagrangeApex], (PHP free of charge not Java Servlets).

-----
NIN - Reptile (Reciprocating Marine engine)

Girvo 
10/6/08 9:07:10 PM
Immortal

PHP should be OOP free? Or at least minimal OOP? Bugger off, I like my frameworks. Makes code maintanable.

-----
Quote by s.o.u.p!
I pull out my 27" telescopic baton and wave it menacingly at possums sometimes because I'm lonely and lame.




Euphemism?
LagrangeApex 
10/6/08 10:24:05 PM
Champion

Oh don't get me wrong, I love my MVC frameworks, and I especially love activerecord, but for the above example and things of that ilk, PHP really shines in all its interpreted, procedural glory.

-----
-=-=-=-=-

  1 | 2  | Next Page 
Forums | Web Design