private string Download(string sID)
{
string sql = "select FileName,File FROM 文件列表 where FileID = " + sID;
SqlConnection con = ZFilesList.ZOpenConnecttion();
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
string sFileName = reader["FileName"].ToString();
Response.ClearHeaders();
Response.Clear();
Response.Expires = 0;
Response.Buffer = true;
Response.Charset = "gb2312";
//把 attachment 改为 on
Response.AppendHeader("Content-Disposition", "attachment;filename=\"" +Server.UrlEncode(sFileName) + "\"");//很重要不然显示出来的是乱码
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Page.EnableViewState = false;
string[] keyValue = sFileName.Split(".".ToCharArray());
if(keyValue.Length>=2)
{
string type = (string)((keyValue[keyValue.Length - 1]).ToString());
Response.ContentType = GetResponseContentType(type);
}
byte[] bt = (byte[])reader["File"];
Response.OutputStream.Write(bt, 0, bt.Length);
}
reader.Close();
reader.Dispose();
cmd.Dispose();
}
public string GetResponseContentType(string fileType)
{
string type;
switch (fileType)
{
case "doc": type = "application/vnd.ms-word"; break;
case "jpg": type = "image/jpeg"; break;
case "txt": type = "application/vnd.ms-word"; break;
case "tif||tiff": type = "text/plain"; break;
case "gif": type = "image/gif"; break;
case "bmp": type = "image/bmp"; break;
case "swf": type = "application/x-shockwave-flash"; break;
case "zip": type = "application/zip"; break;
case "sql": type = "application/vnd.ms-word"; break;
case "dwg": type = "application/autocad"; break;
case "pdf": type = "application/pdf"; break;
case "ppt": type = "appication/powerpoint"; break;
default: type = "text/html"; break;
}
return type;
}