zoukankan      html  css  js  c++  java
  • 文件下载

    界面 <a href="/Handler/FileDownLoadHandler.ashx?filePath=~ emplateclientTemplate.xlsx" target="content">模板下载</a>

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Web;
    using System.Web.SessionState;

    namespace Web.Handler
    {
    /// <summary>
    /// FileDownLoadHandler 的摘要说明
    /// </summary>
    public class FileDownLoadHandler : IHttpHandler, IRequiresSessionState
    {
    //目前只支持 excel 下载,后续可增加参数进行判断,以增加多种文件下载
    public void ProcessRequest(HttpContext context)
    {
    if(context.Request.QueryString["filePath"]!=null)
    {
    string excelFile = HttpContext.Current.Server.MapPath(HttpContext.Current.Server.UrlDecode(context.Request.QueryString["filePath"].Trim()));
    try {
    FileInfo fi = new FileInfo(excelFile);//excelFile为文件在服务器上的地址
    HttpResponse contextResponse = HttpContext.Current.Response;
    contextResponse.Clear();
    contextResponse.Buffer = true;
    contextResponse.Charset = "UTF-8"; //设置了类型为中文防止乱码的出现
    contextResponse.AppendHeader("Content-Disposition", String.Format("attachment;filename={0}", DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx")); //定义输出文件和文件名
    contextResponse.AppendHeader("Content-Length", fi.Length.ToString());
    contextResponse.ContentEncoding = Encoding.Default;
    contextResponse.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。

    contextResponse.WriteFile(fi.FullName);
    contextResponse.Flush();
    contextResponse.End();
    }
    catch (Exception ee) {
    Log.Log4NetUtility.Error(this, ee.Message);
    }
    }
    }

    public bool IsReusable
    {
    get
    {
    return false;
    }
    }
    }
    }

  • 相关阅读:
    机器学习&数据挖掘笔记_16(常见面试之机器学习算法思想简单梳理)
    linux_shell_根据网站来源分桶
    mac_Alfred_快捷设置
    linux_无密登录
    crawler_Docker_解决用 JavaScript 框架开发的 Web 站点抓取
    linux下查看最消耗CPU、内存的进程
    绕过登陆常用万能密码
    ctf比赛linux文件监控和恢复shell
    Python爬虫之Selenium的常用方法
    CTF比赛时准备的一些shell命令
  • 原文地址:https://www.cnblogs.com/zhang-wenbin/p/5853830.html
Copyright © 2011-2022 走看看