---out.aspx---
<%@ Page language="c#" Inherits="_out" CodeFile="DocView.aspx.cs"%>
---out.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public partial class _out
{
protected void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
string mime=GetMime(); //MIME类型
string filename=GetFileName(); //在客户端显示的文件名
if (mime.IndexOf("text") < 0)
{
string filename = filename.Replace(":",":").Replace("/","").Replace("//","") ; //去掉不适合做文件名的字符
string s = HttpUtility.UrlEncode(System.Text.UTF8Encoding.UTF8.GetBytes(filename)); //编码,以适合 http 传输
Response.AppendHeader("Content-Disposition", "attachment; filename=" + s);
}
byte[] content=GetContent(); //取得内容,可以从文件或数据库取得
Response.BinaryWrite(content); //写入输出流
}
}
}
}