By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,825 Members | 1,697 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,825 IT Pros & Developers. It's quick & easy.

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

P: n/a
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?


Nov 18 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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" <ag*****@kc.rr.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
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?



Nov 18 '05 #2

P: n/a
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 [Microsoft MVP]" <BA************@sympatico.ca> wrote in message
news:e1**************@TK2MSFTNGP14.phx.gbl... 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" <ag*****@kc.rr.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
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?


Nov 18 '05 #3

P: n/a
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" <ag*****@kc.rr.com> a écrit dans le message de
news:uA*************@tk2msftngp13.phx.gbl...
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 [Microsoft MVP]" <BA************@sympatico.ca> wrote in message
news:e1**************@TK2MSFTNGP14.phx.gbl...
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" <ag*****@kc.rr.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
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?



Nov 18 '05 #4

P: n/a
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" wrote:
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 [Microsoft MVP]" <BA************@sympatico.ca> wrote in message
news:e1**************@TK2MSFTNGP14.phx.gbl...
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" <ag*****@kc.rr.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
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?



Nov 18 '05 #5

P: n/a
On Mon, 27 Sep 2004 21:12:04 -0400, "Ken Cox [Microsoft MVP]"
<BA************@sympatico.ca> wrote:
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
Nov 18 '05 #6

P: n/a
Yes without the full pathname attached that does work.

Thanks for the help

Dan

"Ken Cox [Microsoft MVP]" <BA************@sympatico.ca> wrote in message
news:AF**********************************@microsof t.com...
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" wrote:
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 [Microsoft MVP]" <BA************@sympatico.ca> wrote in message
news:e1**************@TK2MSFTNGP14.phx.gbl...
> 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" <ag*****@kc.rr.com> wrote in message
> news:%2****************@TK2MSFTNGP15.phx.gbl...
>> 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?
>>
>>
>>
>>
>>
>>
>


Nov 18 '05 #7

P: n/a
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

--

"Russ" <ru****@eticomm.net> a écrit dans le message de
news:je********************************@4ax.com...
On Mon, 27 Sep 2004 21:12:04 -0400, "Ken Cox [Microsoft MVP]"
<BA************@sympatico.ca> wrote:
Hi Dan,

You need to make sure the browser sees it as an attachment so it will promptthe 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

Nov 18 '05 #8

P: n/a
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

On Tue, 28 Sep 2004 20:33:23 +0200, "Patrice" <no****@nowhere.com>
wrote:
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


Nov 18 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.