Solved

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

Posted on 2000-05-15
2
313 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

728 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