个人使用文件下载方法,留以备用
个人使用文件下载方法,不想记太多的代码,留以备用,嘿嘿也共享哈
个人使用文件下载方法,留以备用
![]()
/**//******************************************************
文件名: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;
![]()
public partial class test_DownLoad : BasePage
![]()
![]()
{
![]()
//对象实例化#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
![]()
protected void Page_Load(object sender, EventArgs e)
![]()
{
![]()
//页面初始化#region //页面初始化
//获取系统用户信息
userID = myAccountInfo.ModelSysUser.EmpID;
userName = myAccountInfo.ModelSysUser.EmpName;
string strURL = this.Request.RawUrl;
int nIndex = strURL.IndexOf("type");
//如果大于货等于0则是从控件传过来的下载,否则是其他地方传过来的下载
if (nIndex >= 0)
![]()
{
string FilePath = Request.QueryString["FilePath"].ToString();
string ID = Request.QueryString["ID"].ToString().Trim();
string Type = Request.QueryString["type"].ToString().Trim();
![]()
mSA = bSA.GetModel(Convert.ToInt32(ID));
![]()
string url = "";
if (Type == "FileSystem")
![]()
{
string FileID = mSA.FileCode;
if (FileHelper.IsFile(FileID))
![]()
{
try
![]()
{
IFile oIFile = null;
oIFile = new FileClass().Identify(FileID);
![]()
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)
![]()
{
logObj.WriteLog(LogType.ExceptionLog, "[" + userName + "]" + "执行文件系统下载业务附件操作失败,执行页面:" + Request.ServerVariables["URL"] + ";错误详情:" + ex.Message.ToString(), "系统管理-业务附件-业务附件下载", userID);
}
}
}
else
![]()
{
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);
![]()
}
//从其他地方传过来的下载
else
![]()
{
try
![]()
{
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();
![]()
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;
![]()
//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)
![]()
{
JScript.Alert("下载格式不正确");
logObj.WriteLog(LogType.ExceptionLog, "[" + userName + "]" + "执行普通业务附件下载操作失败,执行页面:" + Request.ServerVariables["URL"] + ";错误详情:" + ex.Message.ToString(), "系统管理-业务附件-业务附件下载", userID);
}
}
#endregion
}
![]()
![]()
//文件下载#region //文件下载
![]()
/**//// <summary>
/// 文件下载
/// </summary>
/// <param name="url"></param>
private void DownLoadFile(string url)
![]()
{
![]()
//文件下载#region //文件下载
try
![]()
{
System.IO.FileInfo toDownload = new System.IO.FileInfo(url);
if (toDownload.Exists)
![]()
{
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(toDownload.Name));
Response.AddHeader("Content-Length", toDownload.Length.ToString());
Response.ContentType = "application/octet-stream";
![]()
this.Response.WriteFile(toDownload.FullName);
}
else
![]()
{
JScript.Alert("文件不存在!");
JScript.CloseWindow(this);
}
logObj.WriteLog(LogType.OperationLog, "[" + userName + "]" + "执行普通业务附件下载操作成功,执行页面:" + Request.ServerVariables["URL"], "系统管理-业务附件-业务附件下载", userID);
}
catch (Exception ex)
![]()
{
logObj.WriteLog(LogType.ExceptionLog, "[" + userName + "]" + "执行普通业务附件下载操作失败,执行页面:" + Request.ServerVariables["URL"] + ";错误详情:" + ex.Message.ToString(), "系统管理-业务附件-业务附件下载", userID);
}
#endregion
}
#endregion
![]()
}

/**//******************************************************
文件名: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;
public partial class test_DownLoad : BasePage

{
//对象实例化#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
protected void Page_Load(object sender, EventArgs e)
{
//页面初始化#region //页面初始化
//获取系统用户信息
userID = myAccountInfo.ModelSysUser.EmpID;
userName = myAccountInfo.ModelSysUser.EmpName;
string strURL = this.Request.RawUrl;
int nIndex = strURL.IndexOf("type");
//如果大于货等于0则是从控件传过来的下载,否则是其他地方传过来的下载
if (nIndex >= 0)
{
string FilePath = Request.QueryString["FilePath"].ToString();
string ID = Request.QueryString["ID"].ToString().Trim();
string Type = Request.QueryString["type"].ToString().Trim();
mSA = bSA.GetModel(Convert.ToInt32(ID));
string url = "";
if (Type == "FileSystem")
{
string FileID = mSA.FileCode;
if (FileHelper.IsFile(FileID))
{
try
{
IFile oIFile = null;
oIFile = new FileClass().Identify(FileID);
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)
{
logObj.WriteLog(LogType.ExceptionLog, "[" + userName + "]" + "执行文件系统下载业务附件操作失败,执行页面:" + Request.ServerVariables["URL"] + ";错误详情:" + ex.Message.ToString(), "系统管理-业务附件-业务附件下载", userID);
}
}
}
else
{
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);
}
//从其他地方传过来的下载
else
{
try
{
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();
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;
//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)
{
JScript.Alert("下载格式不正确");
logObj.WriteLog(LogType.ExceptionLog, "[" + userName + "]" + "执行普通业务附件下载操作失败,执行页面:" + Request.ServerVariables["URL"] + ";错误详情:" + ex.Message.ToString(), "系统管理-业务附件-业务附件下载", userID);
}
}
#endregion
}

//文件下载#region //文件下载
/**//// <summary>
/// 文件下载
/// </summary>
/// <param name="url"></param>
private void DownLoadFile(string url)
{
//文件下载#region //文件下载
try
{
System.IO.FileInfo toDownload = new System.IO.FileInfo(url);
if (toDownload.Exists)
{
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(toDownload.Name));
Response.AddHeader("Content-Length", toDownload.Length.ToString());
Response.ContentType = "application/octet-stream";
this.Response.WriteFile(toDownload.FullName);
}
else
{
JScript.Alert("文件不存在!");
JScript.CloseWindow(this);
}
logObj.WriteLog(LogType.OperationLog, "[" + userName + "]" + "执行普通业务附件下载操作成功,执行页面:" + Request.ServerVariables["URL"], "系统管理-业务附件-业务附件下载", userID);
}
catch (Exception ex)
{
logObj.WriteLog(LogType.ExceptionLog, "[" + userName + "]" + "执行普通业务附件下载操作失败,执行页面:" + Request.ServerVariables["URL"] + ";错误详情:" + ex.Message.ToString(), "系统管理-业务附件-业务附件下载", userID);
}
#endregion
}
#endregion
}

浙公网安备 33010602011771号