Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2000-05-15
2
Medium Priority
?
322 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 225 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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-…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

610 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