Solved

Force Download of .csv file via JS or any other means

Posted on 2000-05-15
2
307 Views
Last Modified: 2008-03-03
I have searched this site and I have found many similar answers to my question:  How can you force a file to be downloaded instead of opening in the browser?  This is exactly what I want to happen.  I have a button on a webpage that points to a csv file.  When the user clicks on it, I want the browser to automatically prompt for save/download instead of displaying the file.

Current Problems (limitations):
1) The filename is created beyond my control.  It will always be *.csv.
2) I am trying to bypass the right-click save or file save as options.

I read somewhere that there was JavaScript that could force a download, but I can't find that code.  Any help is greatly appreciated.

Thanks
0
Comment
Question by:vann_d
2 Comments
 
LVL 1

Accepted Solution

by:
barx earned 75 total points
ID: 2812916
You can use this bit of code, it is in ASP however, not Javascript.  I use a dll called 'BinaryFile.dll' for reading the file in and then writing it out, but I know little about its origins.  You may have to have a look around for it.

dim objBinFile      'BinaryFile.BinaryRead object
dim bData      'holds binary data on the image specified by strSourceFileName
dim strValue      'used to compile the value for the AddHeader call
      
'create object and read image file into variable
set objBinFile = server.CreateObject("BinaryFile.BinaryRead")
bData = objBinFile.readBinaryFile(strSourceFileName)

'set data in value string for the call to Response.AddHeader, attching the destination filename
strValue = "attachment;filename=" & strDestFileName
'change the contenttype
Response.ContentType = "application/octet-stream"
'create a new header called "Content-Disposition" and assign the value of strValue
Response.AddHeader "Content-Disposition", strValue
'do a Binarywrite using the data file, this triggers the browser to popup the save dialog
Response.BinaryWrite(bData)
      
set bFile = nothing

Post back if you need more help!
Here are also a couple of references
http://www.nic.mil/ftp/rfc/rfc2183.txt
Microsoft Knowledge Base, Article ID Q182315
0
 

Author Comment

by:vann_d
ID: 2813873
thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now