File Download Dialog on Page Load

  • Thread starter Thread starter ckarbass
  • Start date Start date
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.
 
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
 
Back
Top