// 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);
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);
    }

   }

  }

利用上面的方法输出的图片,可以控制是查看还是下载.
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); 
}
 
}
 
}
 

本文转载:CSDN博客