What is easiest way to save a file to a users / client computer

D

DanB

This is probably soooo simple but I can't seem to get it.

I have a text file that I want users to download via a web page.

I want the file to be saved to a default folder (or one that they choose)
on the users / client file system.

I have toyed with a Self-Extracting zip file but the contents of my zip file
changes each time it is downloaded so that invalidates the exe file. Also
the text file is so small it is a waste to zip it.

The IE:Download behavior looks at the text file and displays in the browser.

I've look at client-side scripting using the FileSystem object but I can't
seem to get FSO to open the file on the server. Does anyone know can FSO
open a file or copy a file from the server to the client?

Ftp is always a possibility but how do I get it saved to the client after it
is downloaded?

This should be simple. What direction should I head here?
 
K

Ken Cox [Microsoft MVP]

Hi Dan,

You need to make sure the browser sees it as an attachment so it will prompt
the user. Here's some code. Let us know if it works for you?

Ken
Microsoft MVP [ASP.NET]
Toronto

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
' ************************************************
' Forces a prompted download of a text file
' rather than displaying the text in the browser
' Ken Cox - MVP [ASP.NET]
' September 27, 2004
' ************************************************
' Create a stringbuilder to hold the text to be sent
Dim sb As New System.Text.StringBuilder
' Create a string variable to hold a line of text
Dim strLine As String
'Give the file a name
Dim filename As String = "mytext.txt"
' Get the text file using a stream reader
Dim sr As System.IO.StreamReader = _
New System.IO.StreamReader(Server.MapPath(filename))
' Loop through the text file, line by line
Do
' Read a line of text into the string variable
strLine = sr.ReadLine()
' Add the text content to the stringbuilder
sb.Append(strLine)
' Do this while there's still text
Loop Until strLine Is Nothing
' Close the stream reader
sr.Close()

'Set the appropriate ContentType.
Response.ContentType = "text/plain"
' Add a header telling the browser
' to expect an attachment and give it the
' name of the attachment
Response.AddHeader _
("content-disposition", "attachment; filename=""" & _
filename & """")
'Write the file's content from the stringbuilder
' directly to the HTTP output stream.
Response.Write(sb.ToString)
Response.End()
End Sub
 
D

DanB

Ken,

I never would have come up with this own my own. Thanks for pointing me
in the right direction.
I have tried the code and it gets me a lot closer.

I can't get it to name the attachment correctly. It is giving it the
name of the webform (issue.aspx) and defaulting to save it in my download
folder (my IE default).

I believe the key is in the Response.addHeader portion as shown in the
snippet below:
Response.AddHeader ("content-disposition", "attachment;
filename=""" & _
user_Complete_Path_name & """")

Is "Attachment; Filename =" the correct parameter to set or is there
some other that might work better.

Thanks for your help.

Ken Cox said:
Hi Dan,

You need to make sure the browser sees it as an attachment so it will
prompt the user. Here's some code. Let us know if it works for you?

Ken
Microsoft MVP [ASP.NET]
Toronto

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
' ************************************************
' Forces a prompted download of a text file
' rather than displaying the text in the browser
' Ken Cox - MVP [ASP.NET]
' September 27, 2004
' ************************************************
' Create a stringbuilder to hold the text to be sent
Dim sb As New System.Text.StringBuilder
' Create a string variable to hold a line of text
Dim strLine As String
'Give the file a name
Dim filename As String = "mytext.txt"
' Get the text file using a stream reader
Dim sr As System.IO.StreamReader = _
New System.IO.StreamReader(Server.MapPath(filename))
' Loop through the text file, line by line
Do
' Read a line of text into the string variable
strLine = sr.ReadLine()
' Add the text content to the stringbuilder
sb.Append(strLine)
' Do this while there's still text
Loop Until strLine Is Nothing
' Close the stream reader
sr.Close()

'Set the appropriate ContentType.
Response.ContentType = "text/plain"
' Add a header telling the browser
' to expect an attachment and give it the
' name of the attachment
Response.AddHeader _
("content-disposition", "attachment; filename=""" & _
filename & """")
'Write the file's content from the stringbuilder
' directly to the HTTP output stream.
Response.Write(sb.ToString)
Response.End()
End Sub


DanB said:
This is probably soooo simple but I can't seem to get it.

I have a text file that I want users to download via a web page.

I want the file to be saved to a default folder (or one that they choose)
on the users / client file system.

I have toyed with a Self-Extracting zip file but the contents of my zip
file changes each time it is downloaded so that invalidates the exe file.
Also the text file is so small it is a waste to zip it.

The IE:Download behavior looks at the text file and displays in the
browser.

I've look at client-side scripting using the FileSystem object but I
can't seem to get FSO to open the file on the server. Does anyone know
can FSO open a file or copy a file from the server to the client?

Ftp is always a possibility but how do I get it saved to the client after
it is downloaded?

This should be simple. What direction should I head here?
 
P

Patrice

This is RFC1806.

IMO you can't use a full path. Just use the file name and let the user save
the file at the location HE wants...

Patrice

--

DanB said:
Ken,

I never would have come up with this own my own. Thanks for pointing me
in the right direction.
I have tried the code and it gets me a lot closer.

I can't get it to name the attachment correctly. It is giving it the
name of the webform (issue.aspx) and defaulting to save it in my download
folder (my IE default).

I believe the key is in the Response.addHeader portion as shown in the
snippet below:
Response.AddHeader ("content-disposition", "attachment;
filename=""" & _
user_Complete_Path_name & """")

Is "Attachment; Filename =" the correct parameter to set or is there
some other that might work better.

Thanks for your help.

Ken Cox said:
Hi Dan,

You need to make sure the browser sees it as an attachment so it will
prompt the user. Here's some code. Let us know if it works for you?

Ken
Microsoft MVP [ASP.NET]
Toronto

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
' ************************************************
' Forces a prompted download of a text file
' rather than displaying the text in the browser
' Ken Cox - MVP [ASP.NET]
' September 27, 2004
' ************************************************
' Create a stringbuilder to hold the text to be sent
Dim sb As New System.Text.StringBuilder
' Create a string variable to hold a line of text
Dim strLine As String
'Give the file a name
Dim filename As String = "mytext.txt"
' Get the text file using a stream reader
Dim sr As System.IO.StreamReader = _
New System.IO.StreamReader(Server.MapPath(filename))
' Loop through the text file, line by line
Do
' Read a line of text into the string variable
strLine = sr.ReadLine()
' Add the text content to the stringbuilder
sb.Append(strLine)
' Do this while there's still text
Loop Until strLine Is Nothing
' Close the stream reader
sr.Close()

'Set the appropriate ContentType.
Response.ContentType = "text/plain"
' Add a header telling the browser
' to expect an attachment and give it the
' name of the attachment
Response.AddHeader _
("content-disposition", "attachment; filename=""" & _
filename & """")
'Write the file's content from the stringbuilder
' directly to the HTTP output stream.
Response.Write(sb.ToString)
Response.End()
End Sub


DanB said:
This is probably soooo simple but I can't seem to get it.

I have a text file that I want users to download via a web page.

I want the file to be saved to a default folder (or one that they choose)
on the users / client file system.

I have toyed with a Self-Extracting zip file but the contents of my zip
file changes each time it is downloaded so that invalidates the exe file.
Also the text file is so small it is a waste to zip it.

The IE:Download behavior looks at the text file and displays in the
browser.

I've look at client-side scripting using the FileSystem object but I
can't seem to get FSO to open the file on the server. Does anyone know
can FSO open a file or copy a file from the server to the client?

Ftp is always a possibility but how do I get it saved to the client after
it is downloaded?

This should be simple. What direction should I head here?
 
G

Guest

Hi Dan,

Glad to be of some help!

Don't try to set the path. the browser determines that and you can't control
it from the page.

Just put the filename in where mytext.txt is:

Dim filename As String = "mytext.txt"

Does that work?

Ken

DanB said:
Ken,

I never would have come up with this own my own. Thanks for pointing me
in the right direction.
I have tried the code and it gets me a lot closer.

I can't get it to name the attachment correctly. It is giving it the
name of the webform (issue.aspx) and defaulting to save it in my download
folder (my IE default).

I believe the key is in the Response.addHeader portion as shown in the
snippet below:
Response.AddHeader ("content-disposition", "attachment;
filename=""" & _
user_Complete_Path_name & """")

Is "Attachment; Filename =" the correct parameter to set or is there
some other that might work better.

Thanks for your help.

Ken Cox said:
Hi Dan,

You need to make sure the browser sees it as an attachment so it will
prompt the user. Here's some code. Let us know if it works for you?

Ken
Microsoft MVP [ASP.NET]
Toronto

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
' ************************************************
' Forces a prompted download of a text file
' rather than displaying the text in the browser
' Ken Cox - MVP [ASP.NET]
' September 27, 2004
' ************************************************
' Create a stringbuilder to hold the text to be sent
Dim sb As New System.Text.StringBuilder
' Create a string variable to hold a line of text
Dim strLine As String
'Give the file a name
Dim filename As String = "mytext.txt"
' Get the text file using a stream reader
Dim sr As System.IO.StreamReader = _
New System.IO.StreamReader(Server.MapPath(filename))
' Loop through the text file, line by line
Do
' Read a line of text into the string variable
strLine = sr.ReadLine()
' Add the text content to the stringbuilder
sb.Append(strLine)
' Do this while there's still text
Loop Until strLine Is Nothing
' Close the stream reader
sr.Close()

'Set the appropriate ContentType.
Response.ContentType = "text/plain"
' Add a header telling the browser
' to expect an attachment and give it the
' name of the attachment
Response.AddHeader _
("content-disposition", "attachment; filename=""" & _
filename & """")
'Write the file's content from the stringbuilder
' directly to the HTTP output stream.
Response.Write(sb.ToString)
Response.End()
End Sub


DanB said:
This is probably soooo simple but I can't seem to get it.

I have a text file that I want users to download via a web page.

I want the file to be saved to a default folder (or one that they choose)
on the users / client file system.

I have toyed with a Self-Extracting zip file but the contents of my zip
file changes each time it is downloaded so that invalidates the exe file.
Also the text file is so small it is a waste to zip it.

The IE:Download behavior looks at the text file and displays in the
browser.

I've look at client-side scripting using the FileSystem object but I
can't seem to get FSO to open the file on the server. Does anyone know
can FSO open a file or copy a file from the server to the client?

Ftp is always a possibility but how do I get it saved to the client after
it is downloaded?

This should be simple. What direction should I head here?
 
D

DanB

Yes without the full pathname attached that does work.

Thanks for the help

Dan

Ken Cox said:
Hi Dan,

Glad to be of some help!

Don't try to set the path. the browser determines that and you can't
control
it from the page.

Just put the filename in where mytext.txt is:

Dim filename As String = "mytext.txt"

Does that work?

Ken

DanB said:
Ken,

I never would have come up with this own my own. Thanks for pointing
me
in the right direction.
I have tried the code and it gets me a lot closer.

I can't get it to name the attachment correctly. It is giving it the
name of the webform (issue.aspx) and defaulting to save it in my download
folder (my IE default).

I believe the key is in the Response.addHeader portion as shown in
the
snippet below:
Response.AddHeader ("content-disposition", "attachment;
filename=""" & _
user_Complete_Path_name & """")

Is "Attachment; Filename =" the correct parameter to set or is there
some other that might work better.

Thanks for your help.

Ken Cox said:
Hi Dan,

You need to make sure the browser sees it as an attachment so it will
prompt the user. Here's some code. Let us know if it works for you?

Ken
Microsoft MVP [ASP.NET]
Toronto

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
' ************************************************
' Forces a prompted download of a text file
' rather than displaying the text in the browser
' Ken Cox - MVP [ASP.NET]
' September 27, 2004
' ************************************************
' Create a stringbuilder to hold the text to be sent
Dim sb As New System.Text.StringBuilder
' Create a string variable to hold a line of text
Dim strLine As String
'Give the file a name
Dim filename As String = "mytext.txt"
' Get the text file using a stream reader
Dim sr As System.IO.StreamReader = _
New System.IO.StreamReader(Server.MapPath(filename))
' Loop through the text file, line by line
Do
' Read a line of text into the string variable
strLine = sr.ReadLine()
' Add the text content to the stringbuilder
sb.Append(strLine)
' Do this while there's still text
Loop Until strLine Is Nothing
' Close the stream reader
sr.Close()

'Set the appropriate ContentType.
Response.ContentType = "text/plain"
' Add a header telling the browser
' to expect an attachment and give it the
' name of the attachment
Response.AddHeader _
("content-disposition", "attachment; filename=""" & _
filename & """")
'Write the file's content from the stringbuilder
' directly to the HTTP output stream.
Response.Write(sb.ToString)
Response.End()
End Sub


This is probably soooo simple but I can't seem to get it.

I have a text file that I want users to download via a web page.

I want the file to be saved to a default folder (or one that they
choose)
on the users / client file system.

I have toyed with a Self-Extracting zip file but the contents of my
zip
file changes each time it is downloaded so that invalidates the exe
file.
Also the text file is so small it is a waste to zip it.

The IE:Download behavior looks at the text file and displays in the
browser.

I've look at client-side scripting using the FileSystem object but I
can't seem to get FSO to open the file on the server. Does anyone
know
can FSO open a file or copy a file from the server to the client?

Ftp is always a possibility but how do I get it saved to the client
after
it is downloaded?

This should be simple. What direction should I head here?
 
R

Russ

Hi Dan,

You need to make sure the browser sees it as an attachment so it will prompt
the user. Here's some code...

Ken, please let me chime in here. I also need to do something like
this. In my case I need to allow the client to download multiple
files which must be stored in a known and specified folder, using
their original file names. There must be no user interaction other
than clicking the download button. After clicking the button one or
more files must automatically be downloaded and stored.

My code is in C#. Any help?

Thanks, Russ
 
P

Patrice

You can't for safety reasons...

You'll have to use a control, a java applet, or a .NET control the user will
have to approve (depending on what it does exactly) first...

Patrice



--
 
R

Russ

Patrice, thanks for your comments. I'm not sure what you mean by
"saftey reasons". The clients are connected and identified with a
certificate both ways. Periodically a client action will generate a
number of reports which need to be printed at the client's location.

Surely this is enough of a common business requirement that Microsoft
would have some way to do it??? It sure does not make sense for the
client to have to approve and select a location for each report
individually.

I am very new to internet programming, but I have put together a
system that allows clients to fill out forms and submit them, and see
summary data. They can also set up and modify their client and
employee data online. The only thing missing is printing of the
reports.

But because of my newness, I don't know exactly how to proceed with
this phase. Is it possible to write an active-x control that can
download the reports? I have already tested an active-x control that
can print them, once they are downloaded and in a known location on
the client's computer, but I have not seen any way for the active-x
control to interact directly with the client's code-behind software to
do the download.

Thanks for any insight you may be able to provide.

Regards, Russ
 

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
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top