• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 326
  • Last Modified:

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

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
vann_d
Asked:
vann_d
1 Solution
 
barxCommented:
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
 
vann_dAuthor Commented:
thanks!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now