force file to download and not open

HI,

I’m sure i’ve asked this before but i cant seem to find the answer.

I want the user to download a pdf, but i dont want it to open it just to download it
is that possible. it always seems to open the pdf in the current tab .

Do you have control over the headers that the server writes? I.e., the machine where the pdf sits?

How did you implement this? Is WebFile.ForceDownload = True for the file?

https://documentation.xojo.com/api/web/webfile.html

Sometimes whether it downloads or not depends on the whim of the browser. If you zip any file first, it will force it to download too. Not recommended for large PDFs (due to time to compress), though it can be done, as they are already compressed.

ForceDownload changes download to tell browser to save it as file.
But if browser knows file extension, it may decide otherwise and prefer to show inline.

i think David and Christian are correct - it doesn’t matter what my webapp does, the browser will decide. I even changed my settings in chrome to force pdf downloads and they display in the browser!

i am displaying a listbox of invoices and the user clicks on the ones they want and then clicks download pdf. problem is once the browser displays the pdf, its in the same tab and then the back button moves the user back to my login page.

i cant think of a way round this, they just want to click on the row ( 20 or 30 times) and download the pdf for later processing.
on other sites than mine though, they want to display the pdfs of course.

its such a pain!

[quote=422824:@Paul Lefebvre]How did you implement this? Is WebFile.ForceDownload = True for the file?

https://documentation.xojo.com/api/web/webfile.html[/quote]

yes, its set to download= true, chrome overrides that though. makes it very hard work for the user to download 20 pdf’s :frowning:

What happens when you use a clean install of Chrome with no plugins? I ask because a quick test on my machine shows that Chrome downloads the PDF when I set WebFile.ForceDownload to True.

Interesting. I will have to test. Although I suspect that I’m trying to push water uphill.

Perhaps the ‘problem’ is that once the user has followed the url to the pdf , the back button needs to return them to where they were. Perhaps it’s just a question of maintaining state. Then the user can do what they like with the browser settings for pdf.

forcedownload will do three things:

mime type changed to “application/octet-stream”
Content-Disposition set to "attachment; filename= with the filename you gave.
Content-Description set to “File Transfer”

The browser is Kindly asked to download with the Content-Disposition as well with the mime type.
But due to a lot of bad configured web servers, they may show something with .pdf file extensions.

Have you tried to download with a dummy name, and then rename?

Just a suggestion!

[quote=423268:@Peter Malmkjær]Have you tried to download with a dummy name, and then rename?

Just a suggestion![/quote]
Once the browser has downloaded the dummy file, you cannot access the file system to rename it — how are you suggesting the renaming happens?

ForceDownload = true should do it.
If not, you can zip file on server and deliver zip file and hope for auto expand.
You can even do different behavior for different browsers.

forcedownload is definitely true.

the user doesn’t want to unzip them, they are trying to speed up the workflow.

I’m currently on 2017r1.1 - is it possible that this might be something that has changed since then?

i turned off the chrome extension jspdf and now it opens in adobe!

i don’t have the adobe extension included so it might be the desktop app. this is getting weirder!

Maybe; https://www.monkeybreadsoftware.net/files-macfileoperationmbs-shared-method.shtml#1

[quote=423285:@Russ Lunn]i turned off the chrome extension jspdf and now it opens in adobe!

i don’t have the adobe extension included so it might be the desktop app. this is getting weirder![/quote]

I added PDF JS to Chrome. After doing so it ignored ForceDownload and displayed the PDF in the browser. So that’s the culprit.

As to Adobe Acrobat opening the file: browsers are commonly configured to ‘open safe files.’ I personally hate this behavior, but it’s something you have no control over from a web site. If a user wants to just click rows and download the PDFs they must disable PDF Viewer extensions and any options to process files after completion.

Edit: maybe the users would be more open to zip downloads if you had them select all the PDFs they want, then sent them as one zip file. They would only have to unzip the one file. Just a thought because zip files are the only way I can see around this.

Something else to try…

After setting up the webfile, set it’s Mime Type to:

application/octet-stream

But that is what forcedownload does already!?

I would have thought the same.

It seemed to work! Now instead of opening in the same tab it downloads and opens in adobe on the desktop. I think that is something in Windows 10 though. Perhaps I’m wrong.