Create file client side with VB.NET and ASP.NET

  • Thread starter Thread starter Russ Green
  • Start date Start date
R

Russ Green

I need help writing a method to create a simple text file. I want that file
to be saved to a local drive (client side of my asp.net app). The filename
will always be the same "license.txt" but the content will be different for
each user. I don't really want to create the file on my server and them have
them downloaded it. I would much rather use a stringbuilder and the save the
contents of the stringbuilder to the users local drive (propted of course).

TIA

Russ
 
Russ said:
I need help writing a method to create a simple text file. I want that file
to be saved to a local drive (client side of my asp.net app). The filename
will always be the same "license.txt" but the content will be different for
each user. I don't really want to create the file on my server and them have
them downloaded it. I would much rather use a stringbuilder and the save the
contents of the stringbuilder to the users local drive (propted of course).

TIA

Russ

The classic download/upload example:

http://www.ondotnet.com/pub/a/dotnet/2002/04/01/asp.html

Instead of WriteFile, just Write the builder.ToString()
 
Russ,

You can not write even a simple file on the userside with ASPNET with normal
security settings.

However you can add information to the cookies which are normally (in non
cookieless mode) set on the clients computer. You can reread that as well.
(don't forget to set the expire date.

I hope this helps,

Cor
 
I'm not trying to write the file directly to the users hard disk. What I
want to do is allow them the dow2nload the file when they click a link. What
I don't want, is to generate that file physically on my server. II would
like, if possible, to generate that file in memory and then present the user
with the standard download dialog so they can choose to download the file on
their system.
 
Thanks,

I currently have this little test I'm working on. It doesn't make a file and
present the user with the download dialog it just displays the sb string in
the web browser.

Help!!!

Private Sub Page_Load (ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
' Create a stringbuilder to hold the text to be sent
Dim sb As New System.Text.StringBuilder

' Add the text content to the stringbuilder
sb.Append("line 1" & vbcrLf)
sb.Append("line 2" & vbcrLf)

'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
Dim filename As String = "license.lic"
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
 
Russ,

Sorry, however why do you do it in an other way as everybody else.
Everybody shows that page and than the user can decide himself to save it or
not. What is the advantage from the method you want to do.

It seems to me especially in the EU a less juridistic good way

Just my thought,

Cor
 
Well what I'm actually trying to do is generate a license file for a user to
download. The content will be unique to any particular user. I want that
user to login to my site. Click a link and be presented with a dialog for
them to download their very own unique license file. I've seen it done
before.
 
Russ,

Why not create an unique page for them.
They have a save as button on their browser (there is as well javascript to
do that).

Cor
 
In case you missed my message :
http://support.microsoft.com/kb/260519/en-us shows how to raise the "save
as" box.

Addtionaly use just Response.Write to write down the file content (client
side the browser doesn't know where the content comes, it just see incoming
bytes). The article is for ASP but applies as well to ASP.NET...

Patrice
 
This code has just done exactly what I want it to on my PC at home.
Brilliant!! When I tried it in work earlier it wasn't working. Why? What
could stop it working on some browsers?

Private Sub Page_Load (ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

' Create a stringbuilder to hold the text to be sent

Dim sb As New System.Text.StringBuilder

' Add the text content to the stringbuilder

sb.Append("<?xml version=1.0 encoding=UTF-8 ?>" & vbcrLf)

sb.Append("<configuration>" & vbcrLf)

sb.Append("</configuration>" & vbcrLf)

' Set the appropriate ContentType.

Response.ContentType = "text/xml"

' Add a header telling the browser

' to expect an attachment and give it the

' name of the attachment

Dim filename As String = "license.xml"

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 article list that ie 4.01 is know to have problems with this.

How does it fail ? (it displays the XML ?)

IMO a browser could perhaps just check the content type and handle this
regardless of the content-disposition header... I would try without with the
content-type or with a fictionous content type. I would try also a different
extension for the filename (just random tries to try to undestand what is
the element that causes the problem).

Patrice

--
 
Back
Top