Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

forced download - on ie

Status
Not open for further replies.

seantuk

Programmer
Mar 22, 2005
62
0
0
GB
i'm writing some code to download a jpeg. the jpeg is passed in the url
Code:
PictureLoader.aspx?picture=images/image.jpg

there is nothing in the aspx file except
Code:
<%@ Page language="c#" Codebehind="PictureLoader.aspx.cs" AutoEventWireup="false" Inherits="MySolution.PictureLoader" %>

here is the important bit
Code:
private void Page_Load(object sender, System.EventArgs e)
{
  Response.Cache.SetExpires(DateTime.Now);
  Response.Cache.SetCacheability(HttpCacheability.NoCache);
  Response.Cache.SetValidUntilExpires(false);

  if(!IsPostBack)
  {
    downloadPic(Request.Params["picture"]);
  }
}

private void downloadPic(string strFileName)
{
  FileStream fsStream;
  long iFileSize;

  fsStream = new FileStream(Server.MapPath(strFileName), FileMode.Open);
  iFileSize = fsStream.Length;

  byte[] Buffer = new byte[iFileSize];
  fsStream.Read(Buffer, 0, (int)iFileSize);
  fsStream.Close();

  Response.AddHeader("Content-Disposition", "attachment; filename=" + strFileName);
  Response.AddHeader("Content-Length", iFileSize.ToString());
  Response.Charset = "UTF-8";
  Response.ContentType = "image/jpeg";

  Response.BinaryWrite(Buffer);
  Response.Flush();
}

it works perfectly on firefox. but on internet explorer, the save file dialog box says that the file is 'images_jpg', and the type is 'HTML Document'. then, when i click save, i get told 'ie wasn't able to open this site. the site can't be found' (paraphrased).

the response header says
Code:
HTTP/1.1 200 OK
Server: Microsoft-IIS/5.0
Date: Fri, 16 Sep 2005 11:34:31 GMT
X-Powered-By: ASP.NET
X-AspNet-Version: 1.1.4322
Content-Disposition: attachment; filename=ranges/jakarta.jpg
Cache-Control: no-cache
Pragma: no-cache
Expires: -1
Content-Type: image/jpeg; charset=UTF-8
Content-Length: 44402
which looks right to me.

what do i need to do? does any body have either some working code for this, or an idea of what i need to change? ta
 
oh yes, whoops. actually i solved it about 20 minutes ago. i just had to get rid of..

Code:
Cache-Control: no-cache
Pragma: no-cache
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top