下载
我的一个下载程序的代码, 仅供参考:
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;
using System.IO;
namespace DDMS
{
/// <summary>
/// docDownload 的摘要说明。
/// </summary>
public class docDownload : PageBase
{
private void Page_Load(object sender, System.EventArgs e)
{
string attachId = Request.QueryString["id"];
DataRow dr = SqlScope.ExecuteDataRow(@"
select
contentType,
fileName,
fileSize,
fileExt,
saveFileName
from
Attach
where
id = @p0
",
attachId);
if (dr == null)
{
return;
}
string filePath = Config.UploadPath + dr["saveFileName"].ToString();
// 文件不存在
if (File.Exists(filePath) == false)
{
Response.Write("找不到文件.");
return;
}
string contentType;
switch (dr["fileExt"].ToString().ToLower())
{
case ".asf":
contentType = "video/x-ms-asf";
break;
case ".avi":
contentType = "video/avi";
break;
case ".doc":
contentType = "application/msword";
break;
case ".zip":
contentType = "application/zip";
break;
case ".xls":
contentType = "application/vnd.ms-excel";
break;
case ".gif":
contentType = "image/gif";
break;
case ".jpg":
contentType = "image/jpeg";
break;
case ".jpeg":
contentType = "image/jpeg";
break;
case ".wav":
contentType = "audio/wav";
break;
case ".mp3":
contentType = "audio/mpeg3";
break;
case ".mpg":
contentType = "video/mpeg";
break;
case ".mpeg":
contentType = "video/mpeg";
break;
case ".rtf":
contentType = "application/rtf";
break;
case ".txt":
contentType = "text/html";
break;
default:
contentType = "application/octet-stream";
break;
}
FileStream fs = File.OpenRead(filePath);
byte[] content = new byte[fs.Length];
fs.Read(content, 0, (int) fs.Length);
Response.Clear();
Response.AddHeader("Content-Type", contentType);
Response.Charset = "GB2312";
if (contentType == "application/octet-stream")
{
Response.AddHeader("Content-Disposition", "attachment;filename=" + dr["fileName"].ToString());
Response.AddHeader("Content-Length", fs.Length.ToString());
}
Response.BinaryWrite(content);
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
浙公网安备 33010602011771号