Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"save to hard disk ..." from client browser

Status
Not open for further replies.

cflis

Programmer
Jul 29, 2000
2
UA
I need to write a page with many liks to files.
When user click to links "Save..." dialog box must
be shown to user
in case .zip or .exe files - this work,
but when I click on link to .jpg or .gif or other graphics files browser show this file
what can I do in order to enable "save to hard disk ..." dialog box when user click on link to file which have any extension ???
 
I don't believe there is any reliable way to do this. The reason the browser gives the user a "save-as" dialog is because a link points to a file it doesn't understand. When you link to any type of file it can display or display with a plug-in, it will show the file and not give the visitor the option. There may be some new tag that allows this but I don't think you'll find one that works across all browsers reliably. I think you would be better off giving instructions such as "In Netscape, right click and choose save or in IE...".

GJ
 
<cfcontent> will solve the problem. Build a hidden frame or something and in Javascript address the jpeg in the hidden frame with cfcontent. It will work.
 
Yes, <cfcontent> is the way to go. Change the download link on your page from href=&quot;files/myjpegfile.jpg&quot; to &quot;files/download.cfm?filename=myjpegfile.jpg&quot;. Then, write download.cfm like this:
Code:
<cfif IsDefined(&quot;URL.filename&quot;)>

   <cfscript>
      // Known MIME Content-Types
      mimeTypes = StructNew();
      mimeTypes[&quot;jpg&quot;] = &quot;image/jpeg&quot;;
      mimeTypes[&quot;gif&quot;] = &quot;image/gif&quot;;
      mimeTypes[&quot;zip&quot;] = &quot;application/zip&quot;;
      mimeTypes[&quot;exe&quot;] = &quot;application/octet-stream&quot;;

      // Get the extension of the file specified
      fileExt = ListLast(URL.filename,&quot;.&quot;);
   </cfscript>

   <!--- Do we know the MIME type for this file? --->
   <cfif ListFind(StructKeyList(mimeTypes),fileExt)>
      <cftry>
         <!--- Send the MIME type and file contents --->
         <cfcontent 
             type=&quot;#mimeTypes[fileExt]#&quot;
             file=&quot;#URL.filename#&quot;
             reset=&quot;YES&quot;>
         <!--- Download the file instead of displaying it inline --->
         <cfheader
             name=&quot;Content-Disposition&quot;
             value=&quot;attachment;filename=#URL.filename#&quot;>
         <cfset fileWasSent = &quot;YES&quot;>
         <cfcatch>
            <cfset fileWasSent = &quot;NO&quot;>
         </cfcatch>
      </cftry>
   <cfelse>
      <cfset fileWasSent = &quot;NO&quot;>
   </cfif>
<cfelse>
   <cfset fileWasSent = &quot;NO&quot;>
</cfif>

<!--- If we couldn't send the file, send a 404 instead --->
<cfif NOT fileWasSent>
   <cfheader
       statuscode=&quot;404&quot;
       statustext=&quot;File Not Found&quot;>
</cfif>
You'll need to expand the list of MIME types to include all the files you'll be offering for download. It might be easier to store them in a database. Also, you should implement some security to control what the user can download -- we put all the files we're offerring for download in a database; then, download.cfm accepts not a filename as a parameter, but an ID value for the file.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top