Connecting Tech Pros Worldwide Forums | Help | Site Map

Download file

Vishal
Guest
 
Posts: n/a
#1: Nov 19 '05
Hello,

I am creating an html file in my web form. After the
file has been created I want the user to show the
messagebox to download this file. Once he has downloaded
or canceled the file, then I will delete it. Now my
problem is how do I show the messagebox, you know when you
download an exe you get the download messagebox.

Thanks

Brian Brown
Guest
 
Posts: n/a
#2: Nov 19 '05

re: Download file


Vishal,

One way to accomplish this is
1) change the content type to what the file represents (e.g. Excel, Word,
etc..)
2) create file in memory and populate it with data
3) write it out to the browser
If they are using a recent version of IE they may not get the download
dialog box but the file will display directly in the browser. The user can
then go to File->Save As.. and save the file however they want. I have
posted a sample for you below.

Hope this helps.
-----------------------------
using System;
....
using System.IO;

Response.ContentType = "application/vnd.ms-excel";
Label1.Text = DateTime.Now.ToString();
StringWriter tTitlew = new StringWriter();
HtmlTextWriter hTitlew = new HtmlTextWriter(tTitlew);
Label1.RenderControl(hTitlew);
Response.Write(tTitlew.ToString());
Vishal
Guest
 
Posts: n/a
#3: Nov 19 '05

re: Download file


Hey Brian,

thanks for the help. But it doesnt work correctly for
me. I have some probs with the code. Here is what I tried:

Dim tTitlew as new StringWriter()
Try
' loop ...
Dim iLoop as integer
For iLoop = 0 To ...
Dim str as String = ...
tTitlew.Write(strLink)
next

Response.ContentType = "application/text/HTML"
Dim hTitlew as new HtmlTextWriter(tTitlew)
Label1.RenderControl(hTitlew)
Response.Write(tTitlew.ToString())

....

When this part executes, it tries to download my aspx page
instead of the memory one. Can you tell me what i am doing
wrong?

Thanks[color=blue]
>-----Original Message-----
>Vishal,
>
>One way to accomplish this is
>1) change the content type to what the file represents[/color]
(e.g. Excel, Word,[color=blue]
>etc..)
>2) create file in memory and populate it with data
>3) write it out to the browser
>If they are using a recent version of IE they may not get[/color]
the download[color=blue]
>dialog box but the file will display directly in the[/color]
browser. The user can[color=blue]
>then go to File->Save As.. and save the file however they[/color]
want. I have[color=blue]
>posted a sample for you below.
>
>Hope this helps.
>-----------------------------
>using System;
>....
>using System.IO;
>
>Response.ContentType = "application/vnd.ms-excel";
>Label1.Text = DateTime.Now.ToString();
>StringWriter tTitlew = new StringWriter();
>HtmlTextWriter hTitlew = new HtmlTextWriter(tTitlew);
>Label1.RenderControl(hTitlew);
>Response.Write(tTitlew.ToString());
>.
>[/color]
Brian Brown
Guest
 
Posts: n/a
#4: Nov 19 '05

re: Download file


Hi Vishal,

Your code looks fine to me. The code needs to be in a page by itself.
Basically you will have a controlling page that has a link that targets the
download page into a new window. The download page will be a blank page
(with a label control on it) that gets dynamic content written to it. Once
the page is accessed then the user will be prompted to download the page or
the client’s browser will display it.

Is this what you are trying to accomplish?

I hope this helps.

Vishal
Guest
 
Posts: n/a
#5: Nov 19 '05

re: Download file


>Is this what you are trying to accomplish?

Yes.

Cant I use an existing page, which has also some other
controls in it?

Thanks

[color=blue]
>-----Original Message-----
>Hi Vishal,
>
>Your code looks fine to me. The code needs to be in a [/color]
page by itself. [color=blue]
>Basically you will have a controlling page that has a [/color]
link that targets the [color=blue]
>download page into a new window. The download page will [/color]
be a blank page [color=blue]
>(with a label control on it) that gets dynamic content [/color]
written to it. Once [color=blue]
>the page is accessed then the user will be prompted to [/color]
download the page or [color=blue]
>the clientâ?Ts browser will display it.
>
>Is this what you are trying to accomplish?
>
>I hope this helps.
>
>.
>[/color]
Vishal
Guest
 
Posts: n/a
#6: Nov 19 '05

re: Download file


I tried what you said with a new page and it works
partially. Partially, because the prob is that it shows
the <head><html> etc tags too. I dont need them. I only
want my content...

Vishal

[color=blue]
>-----Original Message-----
>Hi Vishal,
>
>Your code looks fine to me. The code needs to be in a [/color]
page by itself. [color=blue]
>Basically you will have a controlling page that has a [/color]
link that targets the [color=blue]
>download page into a new window. The download page will [/color]
be a blank page [color=blue]
>(with a label control on it) that gets dynamic content [/color]
written to it. Once [color=blue]
>the page is accessed then the user will be prompted to [/color]
download the page or [color=blue]
>the clientâ?Ts browser will display it.
>
>Is this what you are trying to accomplish?
>
>I hope this helps.
>
>.
>[/color]
Brian Brown
Guest
 
Posts: n/a
#7: Nov 19 '05

re: Download file


Vishal,

I have posted a sample for you that will let you use the existing page to
download a file. This version will still create the file in memory
alleviating any saving of temporary files. To test you can place the code
below in a button click event.

I hope this helps.
----------------------
//set up the page
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/x-msdownload";
Response.AppendHeader("Content-Disposition", "attachment;filename=Test.htm");
//create the stream
string str = @"<HTML><HEAD></HEAD><BODY>Hello World!</BODY></HTML>";
byte[] buf = System.Text.Encoding.GetEncoding(1252).GetBytes(st r);
MemoryStream ms = new MemoryStream(buf);
ms.Read(buf,0,int.Parse(ms.Length.ToString()));
ms.Close();
//output the result
Response.BinaryWrite(buf);
Response.End();
Closed Thread