Downloading files...

R

Ruskin Hardie

The help is not very informative here, but it says, that it is possible to
download files, by using the 'a' tag. What I am trying to do, is create a
link, that the user clicks, which will download a file from the server. This
works fine for most files, but for things like TXT and HTM, they open in the
browser window. I want the 'Save As' dialog box to appear, instead...

If I use;
Response.Write "<a href='http://mydomain/myfile.txt'>myfile</a>"
I get the contents of myfile.txt displayed in the browser window. If I use;
Response.Write "<a href='http://mydomain/myfile.mdb'>myfile</a>"
I get the 'save file as' dialog box appear. How do I force the 'save file
as' dialog box to appear for ALL files?

I tried using the 'FileSystemObject' and using the 'Copy' method of GetFile.
But this does not allow you to have a 'user defined' path to save the file
(which is pretty obvious, as this is the server side)... Is there another
function that I can use, or is there a property setting of the anchor tag,
that I need to set?
 
C

Chet

| The help is not very informative here, but it says, that it is
possible to
| download files, by using the 'a' tag. What I am trying to do,
is create a
| link, that the user clicks, which will download a file from the
server. This
| works fine for most files, but for things like TXT and HTM,
they open in the
| browser window. I want the 'Save As' dialog box to appear,
instead...
|
| If I use;
| Response.Write "<a
href='http://mydomain/myfile.txt'>myfile</a>"
| I get the contents of myfile.txt displayed in the browser
window. If I use;
| Response.Write "<a
href='http://mydomain/myfile.mdb'>myfile</a>"
| I get the 'save file as' dialog box appear. How do I force the
'save file
| as' dialog box to appear for ALL files?
|
| I tried using the 'FileSystemObject' and using the 'Copy'
method of GetFile.
| But this does not allow you to have a 'user defined' path to
save the file
| (which is pretty obvious, as this is the server side)... Is
there another
| function that I can use, or is there a property setting of the
anchor tag,
| that I need to set?
|

It's somewhat hard to control your visitor's browsers settings.
Most browsers are set to display .txt and .htm files. Depending
on your server, I believe it's possible to configure some servers
that would send the file different, but then I'm not sure if your
pages would appear (they're .htm probably). You may want to check
with your hosting provider.

One other way is to put the files into a .zip file. Not only will
this download properly, the file size may be greatly reduced.

hth
 
J

jenny mabe

The help is not very informative here, but it says, that it is possible
to download files, by using the 'a' tag. What I am trying to do, is
create a
link, that the user clicks, which will download a file from the server..
This works fine for most files, but for things like TXT and HTM, they
open in the browser window. I want the 'Save As' dialog box to appear,
instead...

the behaviour of the browser is triggered by the file's
content-disposition header. if you are, for example, streaming a txt file
to the user and want to force a download, you can even specify the file to
save it as by placing this line above all of the other code on the page:

Response.AddHeader "content-disposition","attachment; filename=fname.ext"

more information on this technique and how to use it statically can be
found here:
http://support.microsoft.com/defaul...port/kb/articles/Q260/5/19.ASP&NoWebContent=1


jenny
 
C

Chet

| > The help is not very informative here, but it says, that it
is possible
| > to download files, by using the 'a' tag. What I am trying to
do, is
| > create a
| > link, that the user clicks, which will download a file from
the server.
| > This works fine for most files, but for things like TXT and
HTM, they
| > open in the browser window. I want the 'Save As' dialog box
to appear,
| > instead...
|
| the behaviour of the browser is triggered by the file's
| content-disposition header. if you are, for example, streaming
a txt file
| to the user and want to force a download, you can even specify
the file to
| save it as by placing this line above all of the other code on
the page:
|
| Response.AddHeader "content-disposition","attachment;
filename=fname.ext"
|
| more information on this technique and how to use it statically
can be
| found here:
|
|http://support.microsoft.com/default.aspx?scid=http://support.mi
crosoft.com:80/support/kb/articles/Q260/5/19.ASP&NoWebContent=1
|
|
| jenny

Jenny

When you do this dynamically, you would change fname.ext to the
file you wanted download, correct? And you'd place it as below:

<%@ Language=VBScript %>
<% Response.AddHeader "content-disposition","attachment;
filename=sample.htm"
%>
<!DOCTYPE ...>
<html>
<head>
</head>
<body>content of htm file to be downloaded
</body>
</html>

A few quick questions,

1) Could you include asp in the "sample.htm" file (above) that
would be parsed prior to being sent?

2) On the page you referenced, it says, "To apply the header
statically, right-click the document in the Internet Service
Manager...enter the content-disposition header there."

I'm learning, what's the Internet Service Manager. I "presume"
its on the server (IIS)?? I use PWS on Win98SE and haven't ran
across this. I liked the part about the bug in IE4! (ha)

3) On the page you referenced, it also says, "When Internet
Explorer receives the header..." Will this work with other
browsers? i.e., Mozilla, Netscape, Opera...etc.

Thanks for the info in your first reply.
 
J

jenny mabe

When you do this dynamically, you would change fname.ext to the
file you wanted download, correct? And you'd place it as below:

<%@ Language=VBScript %>
<% Response.AddHeader "content-disposition","attachment;
filename=sample.htm"
%>

yes, that is correct.
1) Could you include asp in the "sample.htm" file (above) that
would be parsed prior to being sent?

create the asp page to display just as in any other asp page the contents
you want the user to see when the file is opened -- any asp or html or
anything else you would normally display. you can really put anything here
-- plain text for a txt file, an html table for an excel file (excel will
automatically convert html tables), anything as long as the content-type
header and the file in the content-disposition header are set correctly
for the file.
2) On the page you referenced, it says, "To apply the header
statically, right-click the document in the Internet Service
Manager...enter the content-disposition header there."
I'm learning, what's the Internet Service Manager. I "presume"
its on the server (IIS)?? I use PWS on Win98SE and haven't ran
across this. I liked the part about the bug in IE4! (ha)

yes, that would be IIS. unfortunately, i dont have a 98 machine to check
for your operating system -- whatever application or control panel you use
to designate website properties, directories, etc.
3) On the page you referenced, it also says, "When Internet
Explorer receives the header..." Will this work with other
browsers? i.e., Mozilla, Netscape, Opera...etc.

yes, it should. some relevant info is here:
http://www.onjava.com/pub/a/onjava/excerpt/jebp_3/index3.html


jenny
 
R

Roy Danon

<%
strFileName="force_download.asp" 'file to download

Response.Buffer = True
strFilePath=server.mappath(strFilename)
set fso=createobject("scripting.filesystemobject")
set f=fso.getfile(strfilepath)
strFileSize = f.size
set f=nothing: set fso=nothing
Const adTypeBinary = 1
Response.Clear
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = adTypeBinary
objStream.LoadFromFile strFilePath
strFileType = "file/asp" ' change to the correct content type for your file
Response.AddHeader "Content-Disposition", "attachment; filename=" &
strFileName
Response.AddHeader "Content-Length", strFileSize
Response.Charset = "UTF-8"
Response.ContentType = strFileType
Response.BinaryWrite objStream.Read
Response.Flush
objStream.Close
Set objStream = Nothing
%>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,372
Latest member
LucretiaFo

Latest Threads

Top