zoukankan      html  css  js  c++  java
  • ASP.NET实现下载地址隐藏和简单防盗链

    ASP.NET实现下载地址隐藏和简单防盗链

    此方法可以让下载地址隐藏,并且只能ie下载,一般的下载工具无法下载。

    //获取文件名

    string filename = ((LinkButton)e.CommandSource).Text;

    if (filename != "")
    {

    //得到文件地址
    string path = Server.MapPath("upload/") + filename;
    System.IO.FileInfo file = new System.IO.FileInfo(path);
    if (file.Exists)
    {//设置输出参数并输出文件流
    Response.Clear();

    //设置下载对话框显示的文件名,用Server.UrlEncode方法可以防止下载文件名出现乱码
    Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(file.Name));
    Response.AddHeader("Content-Length", file.Length.ToString());
    Response.ContentType = "application/octet-stream";
    Response.WriteFile(file.FullName);
    Response.End();
    }
    else
    {
    lbErr.Text = "该文件不存在,请联系管理员!";
    }
    }

    附:此方法不能用于下载大型文件。

    MSDN原文说明:

    对大型文件使用此方法时,调用此方法可能导致异常。可以使用此方法的文件大小取决于 Web 服务器的硬件配置。有关更多信息,请参见位于 Microsoft 知识库上的文章 812406“PRB: Response.WriteFile Cannot Download a Large File”(PRB:Response.WriteFile 无法下载大型文件)。

  • 相关阅读:
    my first android test
    VVVVVVVVVV
    my first android test
    my first android test
    my first android test
    ini文件
    ZZZZ
    Standard Exception Classes in Python 1.5
    Python Module of the Week Python Module of the Week
    my first android test
  • 原文地址:https://www.cnblogs.com/absolute8511/p/1649664.html
Copyright © 2011-2022 走看看