个人使用文件下载方法,留以备用

本文介绍了一种业务附件的下载方法,包括从文件系统中下载附件的具体实现方式,并提供了异常处理和日志记录功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

个人使用文件下载方法,留以备用
/**/ /******************************************************    
文件名: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


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值