?
Solved

Download excel file without opening it in a browser.

Posted on 2001-09-04
18
Medium Priority
?
319 Views
Last Modified: 2010-07-27
Here is the situation:
 <A href=file://D:\TestWeb\TheExcelFile.xls>This is the one</A>

How can I let the user click on this link to Excel file and recieve a SaveFile dialog? What happens is that Excel file is opened in the browser.  
0
Comment
Question by:mdanny
[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
  • 7
  • 4
  • 3
  • +3
18 Comments
 
LVL 18

Expert Comment

by:mgfranz
ID: 6453938
Response.ContentType()
0
 
LVL 18

Expert Comment

by:mgfranz
ID: 6453954
Sorry;

content-type header

More details shortly.
0
 
LVL 18

Expert Comment

by:mgfranz
ID: 6453966
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 7

Expert Comment

by:John844
ID: 6454117
I think you will need to create a page.  Make the link point to the new page.  In the page you will need to set the header and then send the binary contents of the file.


forcing browser to save text file(or other files)
in most cases, you will find that the browser is already configured to open a MIME type text
file, so pushing a .txt file to save will have to be controlled via a header.  Try this;

Response.AddHeader "content-disposition","attachment; filename=NewFileName.xls"

More details can be found at http://support.microsoft.com/support/kb/articles/Q260/5/19.ASP



here is some code to give you an idea of how to send the binary info.  The example is meant to be used for images... but you can convert it easily.
0
 
LVL 7

Accepted Solution

by:
John844 earned 600 total points
ID: 6454118
oops, forgot to paste code...

<%@ Language=VBScript %>
<%
response.buffer=true

'tell browser not to cache this page
Response.ExpiresAbsolute = Now() -  1
Response.Expires = 0
Response.CacheControl = "NO-CACHE"


'you can check where the file is being requested from to make sure it is valid
'If request.servervariables("HTTP_REFERER") = www.myserver.com/my/link/to/file.asp then

Dim strFileName                  ' local variable to store fileName
Dim intDotLocation               ' local temp variable to store location of dot in FileName
dim strFileExtension             ' local variable to store file extension

strFileName = Request("File")
'change filename here if it is in another folder
' like strFileName = "/HiddenImages/" & strFileName

'get physical path to file like c:/inetpub/wwwroot/website/file.txt
strFileName = Server.MapPath(strFileName)

 'pull the file extension out of the filename
 intDotLocation = instrrev(strFileName,".")
 FileExtension = right(strFileName, (len(strFileName) - DotLocation))


'Create a stream object
 Dim objStream
 Set objStream = Server.CreateObject("ADODB.Stream")
 
 objStream.Type = 1'adTypeBinary '1
 objStream.Open
 
 'Open the file
 objStream.LoadFromFile strFileName
 
 'set the proper ContentType header based on common file extensions
 select case FileExtension
    case "jpg"
         Response.ContentType = "image/jpeg"
    case "jpeg"
         Response.ContentType = "image/jpeg"
    case "gif"
         Response.ContentType = "image/gif"
    case "pdf"
         Response.ContentType = "application/pdf"
    case "doc"
         Response.ContentType = "application/msword"
    case else
         Response.ContentType = "application/octet-stream"
 end select

 'Output the contents of the stream object
 Response.BinaryWrite objStream.Read
 
 'Clean up....
 objStream.Close
 Set objStream = Nothing

%>
0
 
LVL 18

Expert Comment

by:mgfranz
ID: 6454135
John, that's the same link and code from the link I provided...
0
 
LVL 7

Expert Comment

by:John844
ID: 6454415
just trying to clarify and give examples.  you mentioned using content-type header and I was only pointing out that you need to use content-disposition.  The link just happened to be in the section of code I usually post in these cases.  Not trying anything sneaky here.
John
0
 
LVL 18

Expert Comment

by:mgfranz
ID: 6454420
I know... ;-)
0
 
LVL 1

Expert Comment

by:NathanC
ID: 6455495
just listening- I have a similar issue-
0
 
LVL 1

Author Comment

by:mdanny
ID: 6455578
This code doesn't work.I get an error :

Response object error 'ASP 0156 : 80004005'

Header Error

/TestWeb/Default.asp, line 4

The HTTP headers are already written to the client browser. Any HTTP header modifications must be made before writing page content.
0
 
LVL 1

Author Comment

by:mdanny
ID: 6455616
This code doesn't work.I get an error :

Response object error 'ASP 0156 : 80004005'

Header Error

/TestWeb/Default.asp, line 4

The HTTP headers are already written to the client browser. Any HTTP header modifications must be made before writing page content.
0
 
LVL 1

Author Comment

by:mdanny
ID: 6455636
This code doesn't work.I get an error :

Response object error 'ASP 0156 : 80004005'

Header Error

/TestWeb/Default.asp, line 4

The HTTP headers are already written to the client browser. Any HTTP header modifications must be made before writing page content.
0
 
LVL 1

Author Comment

by:mdanny
ID: 6455639
Sorry for that...
0
 
LVL 9

Expert Comment

by:AlfaNoMore
ID: 6456281
You're not trying to redirect to another page are you?

Usually the fix to HTTP headers being written is to add Response.Buffer = True at the very top of the page (before any actual page content gets written)
0
 
LVL 9

Expert Comment

by:AlfaNoMore
ID: 6456289
Something like

<<start page>>
<html>
<% 'Add Header %>
</html>
<<end page>>

Is not going to work, because the headers and html tags are already printed on the page.
0
 

Expert Comment

by:devenkhatri
ID: 6456454
you just add the following line at the beginning

response.contentType = "application/vndms-word"
0
 
LVL 18

Expert Comment

by:mgfranz
ID: 6457026
devenkhatri, welcome to Experts Exchange, we hope you enjoy yourself and active in discussions.  However, I must inform you that there are a few basic rules that must be adhered to if you want to make friends with the experts here.

First of all, read this; http://www.experts-exchange.com/jsp/cmtyQuestAnswer.jsp
It is full of good innformation.  Second of all, never, ever copy another experts comment and propose it as your "answer" as you have done above.  If you read the comments and followed the thread you would have read that both myself and John already mentioned ContentType.

But even further, your proposed "answer" will do nothing except what mdanny is trying to avoid, all "response.contentType = "application/vndms-word"" does it tell the browser to open the file as an Excel application, if you read the Q mdanny is trying to open the File Save As dialog box.  Which is what the following does;

Response.AddHeader "content-disposition","attachment; filename=NewFileName.xls"

Do us a favor and retract your "answer", then read the question and the comments prior to posting anything, if you have something to add please do.
0
 
LVL 18

Expert Comment

by:mgfranz
ID: 6457109
Remember, Response.ContentType will tell the browser how to handle the file, content-disposition tells the browser to raise the "File Save As" dialog box.  You need to read this; http://support.microsoft.com/support/kb/articles/Q260/5/19.ASP
0

Featured Post

Industry Leaders: 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

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

770 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