Solved

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

Posted on 2000-05-15
2
306 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…
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…

743 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

14 Experts available now in Live!

Get 1:1 Help Now