怎样输出 图片 下载?

ASP.NET可以直接输出文件下载,例如doc,zip,rar这类IIS不能解释的格式,如果是Bmp,Gif,Jepg就不行了,怎样实现其下载呢(出现保存那个对话框)?

    // Identify the file to download including its path.
    string filepath = DownloadFileName;

    
// Identify the file name.
    string filename = System.IO.Path.GetFileName(filepath);

    Response.Clear();

    
// Specify the Type of the downloadable file.
    Response.ContentType = "application/octet-stream";

    
// Set the Default file name in the FileDownload dialog box.
    Response.AddHeader("Content-Disposition""attachment; filename=" + filename);

    Response.Flush();

    
// Download the file.
    Response.WriteFile(filepath);

看了http://www.cnblogs.com/koffer/archive/2004/10/14/52289.htmlt也没有发现答案! 

BTW:Response.WriteFile 无法下载大文件
posted @ 2005-08-28 13:29 卡卡 ^ cacard 阅读(1315) 评论(2)  编辑 收藏 所属分类: ASP.NET

  回复  引用  查看    
#1楼 2005-08-29 09:14 | 涟漪勇      

 private void ViewImage(string fileName, bool forceDownload)
  {
   Response.Clear();
   if(forceDownload)
   {
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
   }
   else
   {
    Response.AppendHeader("Content-Disposition", "filename=" + fileName);
   }

   using(System.Drawing.Image image = System.Drawing.Image.FromFile(MapPath(ImageFolder + "/" + fileName)))
   {
    if(image.RawFormat.Equals(ImageFormat.Bmp))
    {
     Response.ContentType = "image/bmp";
    }
    else
    {
     if(image.RawFormat.Equals(ImageFormat.Gif))
     {
      Response.ContentType = "image/gif";
     }
     else
     {
      if(image.RawFormat.Equals(ImageFormat.Jpeg))
      {
       Response.ContentType = "image/jpeg";
      }
      else
      {
       if(image.RawFormat.Equals(ImageFormat.Png))
       {
        Response.ContentType = "image/png";
       }
       else
       {
        Response.ContentType = "application/octet-stream";
       }
      }
     }

     image.Save(Response.OutputStream, image.RawFormat);
    }
   }
  }
利用上面的方法输出的图片,可以控制是查看还是下载.

  回复  引用    
#2楼 2005-10-13 17:06 | twins [未注册用户]
private void ViewImage(string fileName, bool forceDownload)
{
Response.Clear();
if(forceDownload)
{
Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
}
else
{
Response.AppendHeader("Content-Disposition", "filename=" + fileName);
}

using(System.Drawing.Image image = System.Drawing.Image.FromFile(MapPath(ImageFolder + "/" + fileName)))
{
if(image.RawFormat.Equals(ImageFormat.Bmp))
{
Response.ContentType = "image/bmp";
}
else
{
if(image.RawFormat.Equals(ImageFormat.Gif))
{
Response.ContentType = "image/gif";
}
else
{
if(image.RawFormat.Equals(ImageFormat.Jpeg))
{
Response.ContentType = "image/jpeg";
}
else
{
if(image.RawFormat.Equals(ImageFormat.Png))
{
Response.ContentType = "image/png";
}
else
{
Response.ContentType = "application/octet-stream";
}
}
}

image.Save(Response.OutputStream, image.RawFormat);
}
}
}




This is footer