File Download Dialog on Page Load

C

ckarbass

I'd like for the file download dialog to be launched upon page load.
The workflow or behavior is exactly the same as sourceforge's.

I've searched on google to no avail. Any help would be appreciated.
Thanks.
 
G

Guest

Howdy,

No probs. Different type of content cannot be mixed (well in theory it can,
anyone interested have a look at this topic:
http://groups.google.co.uk/group/mi...mixed+with+html&rnum=3&hl=en#c48b66ab6b35e3db)
therefore, you must use an iframe inside your main document that will serve
file as in following example:
-- main page aspx code --
<iframe runat="server" id="whatever" src="download.aspx?fileid=123"></iframe>
-- end --

-- download.aspx c# code behind --
public partial class Download : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

// get the file path from query string id
// be careful what you serve, do not allow
// any file to be available
// in my example i added jpeg image copied
// to the application's root folder
string filePath = Server.MapPath("~/sunset.jpg");

System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);

Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", String.Format("attachment;
filename=\"{0}\"", filePath));
Response.AddHeader("Content-Length", fileInfo.Length.ToString());
Response.WriteFile(filePath);
Response.End();

}
}
-- end --


Done.

Hope this helps
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top