Download file

V

Vishal

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
 
G

Guest

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());
 
V

Vishal

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
 
G

Guest

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.
 
V

Vishal

Is this what you are trying to accomplish?

Yes.

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

Thanks
 
V

Vishal

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
 
G

Guest

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(str);
MemoryStream ms = new MemoryStream(buf);
ms.Read(buf,0,int.Parse(ms.Length.ToString()));
ms.Close();
//output the result
Response.BinaryWrite(buf);
Response.End();
 

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,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top