Saturday, 29 December 2012 13:54

how to force to download file in asp.net

Written by 
Rate this item
(0 votes)

we know different file types like html,jpeg,gif,pdf,
doc,etc handled by different browsers like IE,
firefox,opera,etc.the situation can come when the
browser can't handle the file type,it will ask
to save or open that file.

sometime you may need to
force your user to download that file though the
browser can open this file type.this requirement can
be accomplished using content-disposition header
in your page response.

the following example shows how to write a gif file
in response & force user only to download the file -

protected void Page_Load(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=image.gif");
Response.ContentType = "image/GIF";
Response.WriteFile(Server.MapPath(@"~/image.gif"));
Response.End();
}

Read 3572 times
Super User

Email This email address is being protected from spambots. You need JavaScript enabled to view it.

Latest discussions

  • No posts to display.