473,327 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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

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
8 2657
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
7
by: Dan V. | last post by:
Situation: I have to connect with my Windows 2000 server using VS.NET 2003 and C# and connect to a remote Linux server at another company's office and query their XML file. Their file may be...
28
by: joe | last post by:
I have a simple .NET application with two or three listViews which are filled with icons and when the user click on the proper item, they display the related images. I use "image = null ; " for all...
2
by: mboyda | last post by:
I have a block of code that useses outputstream to save a pdf to a client computer. It prompts the user with the save dialog, works great but the response finishes after the download completes. ...
4
by: Nikhil Tayal | last post by:
Is there a way to write a file on the client machine from an aspx page? I've a custom query page and need to store the search criteria specified in an XML file on the user machine from my web page...
5
by: Patrick | last post by:
Following on from the excellent example at http://www.c-sharpcorner.com/Code/2003/Sept/ExportASPNetDataGridToExcel.asp on how to save a data-grid to excel file, how can I extend the example such...
4
by: Matt | last post by:
Hi, I would like to save a file locally (on the client computer) in an ASP.net application. It is like the server would return some data that the user can save to a file on his local PC. The ASP...
1
by: poussy-puce | last post by:
Hi, I would like to choose destination path when user download file on her computer... I have code for the download, but I don't find code or component for change the destination on the Save in...
12
by: =?Utf-8?B?RnJlZU5FYXN5?= | last post by:
Hello, the scenario: There's an ASPX page which shows some text and has three buttons at the bottom: Save, Print and Close. Print and close is done by javascript. But how can I save the page...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.