zoukankan      html  css  js  c++  java
  • C#根据path文件地址进行下载

    1:在js文件中写入加密方法

    var Comm = {
        encode: function (text) {
            if (text)
                return new Base64().encode(text);
            else
                return text;
        }

    }

    2:Handler类中返回文件地址:

     如:string path = "/Files/审理意见书.doc";

    3:在js方法中接收并跳转页面进行下载

    function Download(){
          $.post(' ',{action:' '},function(data){

        if(path!=""){

          window.location.href="/Download.aspx?P="+Comm.encode(path);

        }

      });
       }

    4:在Download.aspx也进行后台代码操作

        a:首先写入解密方法

     public  static string Decrypt(string text) {

      byte[] bytes = Convert.FromBase64String(text);

      return Encoding.UTF8.GetString(bytes);

     }

        b:进行文件解析以及下载操作

     protected void Page_Load(object sender, EventArgs e) {

         string path = Decrypt(Parse.ToString(Request["p"]).Replace(" ", "+"));

         string fileName = path.Substring(path.LastIndexOf('/') + 1);

         var filePath = HttpContext.Current.Server.MapPath(path);

         try {

            //判断是否存在此文件

            if (File.Exists(filePath)) {

              FileStream io = File.Open(filePath, FileMode.Open);

              long fileSize = io.Length;
                           byte[] filebit = new byte[(int) fileSize];
                             io.Read(filebit, 0, (int) fileSize);
                           Response.Clear();
                           fileName = HttpUtility.UrlEncode(fileName);
                           Response.ContentType = "application/octet-stream";
                           Response.AddHeader("Content-Disposition", "attachment;fileName=" + fileName);
                           Response.BinaryWrite(filebit);
                           io.Dispose();
                           Response.Flush();
                           Response.End();

            } else {
                            Response.Clear();
                            Response.Write("<script type="text/javascript" src="/Scripts/jquery-1.10.2.js"></script><script>$(function () {alert('文件不存在或已被删除', { icon: 0 }, function () { window.close(); }, function () { window.close(); });});</script>");
                    }

         }catch (Exception ex) {
                    Response.Clear();
                    Response.Write("<script type="text/javascript" src="/Scripts/jquery-1.10.2.js"></script> <script>$(function () {alert('" + ex.Message + "', { icon: 0 }, function () { window.close(); }, function () { window.close(); });});</script>");
                }

      }

  • 相关阅读:
    K8S 强制删除卡在Terminating状态的namespaces 记录
    docke 搭建 SVN服务器
    mysql-5.6--gtid主从配置
    keeplived + haproxy 高可用网络代理-安装配置 centos-7.6
    elasticsearch磁盘定时清理--转载
    HTTPS证书自动生成--certbot
    haproxy2.0-编译安装-centos7.6
    mongodb-定时按日期备份到json文件
    linux日志切割工具 logrotate
    mongodb--arbiter-集群安装
  • 原文地址:https://www.cnblogs.com/recent/p/6066054.html
Copyright © 2011-2022 走看看