个人使用文件下载方法,留以备用
个人使用文件下载方法,不想记太多的代码,留以备用,嘿嘿也共享哈
个人使用文件下载方法,留以备用
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//******************************************************
文件名:DownLoad.aspx.cs
创建人:王春鹏
创建日期:2007-07-19
修改人: 王春鹏
修改日期:2007-08-03
功能描述:系统附件下载页面
******************************************************/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Wisesoft.Common;
using EOSS.KnowledgeManagement.FileSystem;
using System.Collections.Generic;
using Eoss.SystemManage.BLL;
![](/Images/OutliningIndicators/None.gif)
public partial class test_DownLoad : BasePage
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
//对象实例化#region //对象实例化
private Accessory.BLL.System_Accessories bSA = new Accessory.BLL.System_Accessories();
private Accessory.Model.System_Accessories mSA = new Accessory.Model.System_Accessories();
private Eoss.SystemManage.BLL.OperationRecordPopedom bORD = new Eoss.SystemManage.BLL.OperationRecordPopedom();
private Eoss.SystemManage.BLL.Log logObj = new Eoss.SystemManage.BLL.Log();
private string userName = "";
private string userID = "";
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
protected void Page_Load(object sender, EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
//页面初始化#region //页面初始化
//获取系统用户信息
userID = myAccountInfo.ModelSysUser.EmpID;
userName = myAccountInfo.ModelSysUser.EmpName;
string strURL = this.Request.RawUrl;
int nIndex = strURL.IndexOf("type");
//如果大于货等于0则是从控件传过来的下载,否则是其他地方传过来的下载
if (nIndex >= 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string FilePath = Request.QueryString["FilePath"].ToString();
string ID = Request.QueryString["ID"].ToString().Trim();
string Type = Request.QueryString["type"].ToString().Trim();
![](/Images/OutliningIndicators/InBlock.gif)
mSA = bSA.GetModel(Convert.ToInt32(ID));
![](/Images/OutliningIndicators/InBlock.gif)
string url = "";
if (Type == "FileSystem")
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string FileID = mSA.FileCode;
if (FileHelper.IsFile(FileID))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
IFile oIFile = null;
oIFile = new FileClass().Identify(FileID);
![](/Images/OutliningIndicators/InBlock.gif)
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(oIFile.Name));
Response.AddHeader("Content-Length", oIFile.Length.ToString());
Response.ContentType = "application/octet-stream";
this.Response.BinaryWrite(oIFile.OpenBinaryAsByte());
Response.End();
logObj.WriteLog(LogType.OperationLog, "[" + userName + "]" + "执行从文件系统下载业务附件操作成功,执行页面:" + Request.ServerVariables["URL"], "系统管理-业务附件-业务附件下载", userID);
}
catch (Exception ex)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
logObj.WriteLog(LogType.ExceptionLog, "[" + userName + "]" + "执行文件系统下载业务附件操作失败,执行页面:" + Request.ServerVariables["URL"] + ";错误详情:" + ex.Message.ToString(), "系统管理-业务附件-业务附件下载", userID);
}
}
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
url = this.Server.MapPath("~") + FilePath.Trim() + "\\" + mSA.SysFileName.Trim();
//if (mSA.FileSize > 10000)
//{
// //url = "~/" + FilePath.Trim() + "/" + mSA.SysFileName.Trim();
// //url = url.Replace("\\", "/");
// this.Response.TransmitFile(url);
// //this.Response.Redirect(url);
// this.Response.End();
//}
//else
//{
this.DownLoadFile(url);
//}
//this.DownLoadFile(url);
}
//this.Response.Write(url);
![](/Images/OutliningIndicators/InBlock.gif)
}
//从其他地方传过来的下载
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string OperationCode = Request.QueryString["OperationCode"].ToString();
string OperationID = Request.QueryString["OperationID"].ToString();
Accessory.BLL.System_AccOperation bSao = new Accessory.BLL.System_AccOperation();
Accessory.Model.System_AccOperation mSao = new Accessory.Model.System_AccOperation();
![](/Images/OutliningIndicators/InBlock.gif)
IList modelList = bSA.GetListArray(" OperationCode ='" + OperationCode + "' and OperationID = '" + OperationID + "'");
mSA = (Accessory.Model.System_Accessories)modelList[0];
mSao = bSao.GetModel(OperationCode);
string url = this.Server.MapPath("~") + mSao.SavePath + "\\" + mSA.SysFileName;
![](/Images/OutliningIndicators/InBlock.gif)
//if (mSA.FileSize > 10000)
//{
// //Server.Redirect(url);
// this.Response.Redirect(url);
//}
//else
//{
this.DownLoadFile(url);
//}
logObj.WriteLog(LogType.OperationLog, "[" + userName + "]" + "执行普通业务附件下载操作成功,执行页面:" + Request.ServerVariables["URL"], "系统管理-业务附件-业务附件下载", userID);
}
catch(Exception ex)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
JScript.Alert("下载格式不正确");
logObj.WriteLog(LogType.ExceptionLog, "[" + userName + "]" + "执行普通业务附件下载操作失败,执行页面:" + Request.ServerVariables["URL"] + ";错误详情:" + ex.Message.ToString(), "系统管理-业务附件-业务附件下载", userID);
}
}
#endregion
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
//文件下载#region //文件下载
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 文件下载
/// </summary>
/// <param name="url"></param>
private void DownLoadFile(string url)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
//文件下载#region //文件下载
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
System.IO.FileInfo toDownload = new System.IO.FileInfo(url);
if (toDownload.Exists)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(toDownload.Name));
Response.AddHeader("Content-Length", toDownload.Length.ToString());
Response.ContentType = "application/octet-stream";
![](/Images/OutliningIndicators/InBlock.gif)
this.Response.WriteFile(toDownload.FullName);
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
JScript.Alert("文件不存在!");
JScript.CloseWindow(this);
}
logObj.WriteLog(LogType.OperationLog, "[" + userName + "]" + "执行普通业务附件下载操作成功,执行页面:" + Request.ServerVariables["URL"], "系统管理-业务附件-业务附件下载", userID);
}
catch (Exception ex)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
logObj.WriteLog(LogType.ExceptionLog, "[" + userName + "]" + "执行普通业务附件下载操作失败,执行页面:" + Request.ServerVariables["URL"] + ";错误详情:" + ex.Message.ToString(), "系统管理-业务附件-业务附件下载", userID);
}
#endregion
}
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)