Download image as normal file

V

Victor

Hi.
Is there a good way to let user can download image as a normal file? Like
like the image then popup a messagebox ask user whether he want to save it
or open it?

Cheers
Victor
 
R

Rick Strahl [MVP]

Hi Victor,

if you need to have the file downloaded from the ASP.NET site with a Save As
dialog you can manipulate the headers. Basically you can send the file to
the client yourself with Response.TransmitFile() and then add the
appropriate Content-Type and Content-Disposition.

Something like this:

Response.ContentType = "image/jpg"
Response.AppendHeader("Content-Disposition","attachment;
filename=MyFile.jpg");
Response.TransmitFile( Server.MapPath("~/images/MyFile.jpg") );


+++ Rick ---
 
S

Spring Liu

Hi.
Is there a good way to let user can download image as a normal file? Like
like the image then popup a messagebox ask user whether he want to save it
or open it?

Cheers
Victor

download.aspx:

public partial class download : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{
string filePath = Server.MapPath("~") + "\\test.jpg";

if (File.Exists(filePath))
{
Response.AddHeader("Content-Disposition", "attachment;
filename=test.jpg"); // make sure keep this
Response.TransmitFile(filePath);
}
else
{
Response.Write("No such file : " + filePath);
}
}

}
}

Default.aspx:

<a href="download.aspx?id=1">image link</a>

hope helpful

Spring
 

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,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top