Save As Function

Explorer ,
Apr 02, 2009 Apr 02, 2009

Copy link to clipboard

Copied

I have several files listed on my .cfm using <cfdirectory>, this works perfect, lists, the .pdf's, .xls, etc. Now they open but I don't want my users to open them, I want them to be forced in Saving these files onto their own machine then they can open them. I tried using <cfcontent> and <cfheader> but it's not working the way I want. Right now I'm using the attached code, we like this formatting but I can't figure out how to get these documents to save. Any ideas? Thanks.
TOPICS
Advanced techniques

Views

3.5K

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 02, 2009 Apr 02, 2009

Copy link to clipboard

Copied

still_smiling wrote:
> I have several files listed on my .cfm using <cfdirectory>, this works perfect,
> lists, the .pdf's, .xls, etc. Now they open but I don't want my users to open
> them, I want them to be forced in Saving these files onto their own machine
> then they can open them.

To be exact, these files *are* saved on the users machine before they
are opened. Albeit in the browsers 'temporary files' directory of which
many users are not very aware.

> I tried using <cfcontent> and <cfheader> but it's not working the way I want.

You can use <cfcontent...> and <cfheader...> to do this, but you must
use an unknown mime type for the content. If the browser knows the mime
type it will use any plug-ins it has been configured to use and you have
no control over this. But if the browser does not know the mime type
the only thing it can do is offer the user to download the file.



Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 07, 2009 Apr 07, 2009

Copy link to clipboard

Copied

Still can't get this to work properly so I'm going to post my code and see if anyone can help out with this "Save As" function.


The first page has the following code:

The SQL statments pulls the files so the user can select which file they wish to save. It could be a .pdf, .xls or even a .qbw file

<a href="save.cfm?filename=#dir.name#" target="_blank"><font face="Arial"><strong>#dir.name#</strong></font></a>

The second page, save.cfm has the following code:

<cfheader name="Content-Disposition" value="attachment; filename=#filename#">           
<cfcontent type="unknown" file="users/#GetName.file#/#filename#"  deletefile="No">

Not sure if it makes a difference that I'm doing this with an https.

Thanks in advance, this thing is really driving me crazy.

Courtney

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Apr 07, 2009 Apr 07, 2009

Copy link to clipboard

Copied

<cfcontent type="unknown" file="users/#GetName.file#/#filename#"  deletefile="No">

<cfheader name="Content-Disposition" value="attachment; filename=#GetFile.FileName#">
<cfcontent type="application/octet-stream" file="users/#GetName.file#/#filename#"  deletefile="No">

Maybe, you are fighting the Browser builders who do their best to make this automatic for the users and they have built logic that looks at file extensions as well as file types to make a guess on how to handle delivered content.

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 08, 2009 Apr 08, 2009

Copy link to clipboard

Copied

It functions as it should, sort of, the file downloads but when I go to open the .pdf I get the following error message.

Adobe Reader could not open 'myfile.pdf' because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded).

I know that this .pdf file is good. I'm able to open it from the link which works fine to view and I've checked twice to ensure that the file wasn't corrupt and it's not. So, somewhere in the "Save As" function it's screwing up the file. So, now I'm not sure where to go from here. Anyone have any ideas?

Regards

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Apr 09, 2009 Apr 09, 2009

Copy link to clipboard

Copied

Courtney,


You're wanting this to download as a PDF, so I think you'll find the following to work:

<cfheader name="expires" value="#NOW()#">
<cfheader name="content-disposition" value="attachment; filename=#filename#">
<cfcontent type="application/pdf" file="#file#" deletefile="no" reset="no">

If it were some other type, you'd put that type's MIME type and sub type in the "type" attribute of the cfcontent tag.

David

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 09, 2009 Apr 09, 2009

Copy link to clipboard

Copied

Still not functioning as it should, it's weird, when I select the hyperlink it brings up the box to either "Open with" or "Save File" when I select "Open with" and choose Adobe Acrobat it saves just perfectly but if I select the "Save file" it doesn't work at all, I get that same error. I even looked at the copy up on the server and checked it's header as GrumpyJoe suggested and it says "%pdf", so maybe somewhere I'm still missing something... I mean the plain link that opens up the pdf in a seperate window still works great too.

These two links are my "users_index.cfm" page

The save as link, not working properly...

<a href="save.cfm?filename=#dir.name#"><font face="Arial"><strong>#dir.name#</strong></font></a><br/>

The open in new window link works perfect but not functional as I will have other file extensions such as .xls and .qbw          
<a href="users/#GetName.file#/#dir.name#" target="_blank"><strong>#dir.name#</strong></font></a><br/>

On my "Save.cfm" file I have the following:

<cfheader name="expires" value="#NOW()#">
<cfheader name="content-disposition" value="attachment; filename=#filename#">
<cfcontent type="application/pdf" file="users/#GetName.file#/#filename#" deletefile="no" reset="no">

What in the world am I missing? Geesh... I mean, emails, pdf, different web sites have this functioning, why can't I get this darn thing to work.

Thanks for helping.

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 14, 2009 Apr 14, 2009

Copy link to clipboard

Copied

still_smiling,

I think you are getting confused between server side and client side. Click your save as link, download the file to your computer and open THAT version (the downloaded one) in Notepad - see if it is the same as the one on the server.

Secondly, you are still not setting reset="true" on the code snippet you have here, meaning that html may be contaminating your downloaded file.  As to your open with link, that will of course work because the Webserver itself is serving that and knows to send it uncorrupted, but the save as link is passing through cf - which may add 'cruft' before the code for the PDF.

Have another look at the DOWNLOADED version of the PDF (the one that doesn't open after you have downloaded it) - and set the reset="true" and have another try.

Also, as an aside, is debugging switched on?  Debugging information could well be appended to the downloaded PDF file as well, causing further issues.  Try <cfsetting showdebugoutput="false"> before your cfcontent tag as well.

Message was edited by: GrumpyJoe (spelling)

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 14, 2009 Apr 14, 2009

Copy link to clipboard

Copied

Okay, here is what I get when I look at the "Save File" in Notepad...

An error has occured, please click the back button on your browser.

Here is my code again and I think I've tried everything you suggested.

on the "users_index.cfm" page

This one doesn't work as it should

<a href="save.cfm?filename=#dir.name#"><font face="Arial"><strong>#dir.name#</strong></font></a><br/>
This works fine but not what I need to work.          
<a href="users/#GetName.file#/#dir.name#" target="_blank"><strong>#dir.name#</strong></font></a><br/>

Here is what is on my "save.cfm" file.

<cfheader name="expires" value="#NOW()#">
<cfheader name="content-disposition" value="attachment; filename=#filename#">
<cfsetting showdebugoutput="false">
<cfcontent type="application/pdf" file="users/#GetName.file#/#filename#" deletefile="no" reset="yes">

Could it be that a setting on my server is causing this issue? or any other settings I may be missing?

thanks for helping.

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 15, 2009 Apr 15, 2009

Copy link to clipboard

Copied

Well, looking at the content of the downloaded file (...Error occured...) - sounds like your path may be incorrect and cfcontent is not finding the file to download (or permission errors may have occured).  In save.cfm, before you do the cfcontent call, put the following in:-

<cfoutput>#FileExists('users/#GetName.file#/#filename#')# (users/#GetName.file#/#filename#)</cfoutput><cfabort>


...Actually, looking at that, it is a relative path, not an absolute - this could be the problem...  cfcontent may need the file path all the way from the root directory (c:\ in windows - / in linux).

If you get 'YES' users/xxx/xxx - then I'm stumped.  If you get 'NO' it means your path is incorrect, and you need to work it out with an expandpath() call beforehand. Is the users directory in the same directory as the save.cfm file?  If so, try

<cfcontent type="application/pdf" file="#Expandpath('users/#GetName.file#/#filename#')#" deletefile="no" reset="yes">

which will expand the path from a relative to an absolute path.

GIve those a try and let us know how you get on.

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 15, 2009 Apr 15, 2009

Copy link to clipboard

Copied

Thank you so very much! That was the solution, the ExpandPath.

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 09, 2009 Apr 09, 2009

Copy link to clipboard

Copied

First suggestion, try opening the corrupted file in Notepad or a text editor, and see what is at the top of the file, does it start with 'PDF', or some HTML?

It may be that previous code you render (html headers etc.) is caught in the output stream and is corrupting the downloaded file (everything up to the cfcontent and including is it sent to the end user).  Try setting reset="true" on your cfcontent to flush the output buffer and ensure that your PDF code is the first thing the browser sees.

This has bitten me in the behind more than once...

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines