Member Avatar for arcticM

on the screen there's a button, when user clicks it, a function is called. this function is creating a pdf file, in the end of the function a pdf file is created and saved in tmp folder. I want to add some code at the end of that function that would open this file.
the code might be php, html, javascript, jquery..
I tried this-
"<script> window.open('tmp/file.pdf');</script>";
and it works but it opens up in the browser, and I need it to open up in adobe reader or any program that would normally open it if you just double click the file..

If the browser is navigating to a pdf file, it will probably try to open it with the browser plug-in unless the browser's own config tells it to pass the file to the system default application. If you offer the browser the file for download, I imagine the user will get the opportunity to open the file with their system default program or do whatever they want with it.

Member Avatar for arcticM

but I can't figure out how to do that

  1. I did a search on google for "offer file for download".
  2. I checked the third result
  3. I found the answer:

    content = "abc123";
    document.location = "data:text/octet-stream," + encodeURIComponent(content);
    
  4. I tested it:

        function download(){
        content = "image.jpg.jpg";
        document.location = "data:text/octet-stream," + encodeURIComponent(content);
        }
    

and...

<a href="javascript:;" onclick="download(); return false;">click</a>

...it worked, offering me the file for download.

[edited to fix code format.]

Member Avatar for arcticM

I tried it and it just shows me an empty screen with the path as a text.. horrible

the url is changing to 'data:text/octet-stream,..%2Ftmp%2Ffile.pdf' which doesn't look right to me..

It isn't right, you need to take the path out of the encodeURIComponent() function and just encode the filename.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.