anp.net文件下载,实现隐藏文件地址

asp.net文件下载,实现隐藏文件下载地址

分类: Jquery 文件下载 354人阅读 评论(2) 收藏 举报
文件下载其实很简单。最简单的方式就是直接用a标签指向文件地址,但是这种方式暴露了文件的地址,不适合做积分下载。同时,如果文件为图片或者xml文件的话浏览器默认是打开文件而不是弹出下载窗口。
第二种就是用js实现。
  1. function getFile(id) {  
  2.     jQuery.ajax({  
  3.         type: "POST",  
  4.         dataType: "json",  
  5.         timeout: 3000,  
  6.         url: "/upload/ajax.aspx?act=getfile&r=" + Math.random(),  
  7.         data: { "id": id },  
  8.         beforeSend: function () {  
  9.         },  
  10.         success: function (data) {  
  11.             if (data.statu != "ok") {  
  12.                 alert(data.msg);  
  13.             }  
  14.             else {  
  15.                 window.location.href = data.res;  
  16.             }  
  17.         },  
  18.         error: function (jqXHR, textStatus, errorThrown) {  
  19.             alert(alert(textStatus));  
  20.         }  
  21.     });  
  22. }  
function getFile(id) {
    jQuery.ajax({
        type: "POST",
        dataType: "json",
        timeout: 3000,
        url: "/upload/ajax.aspx?act=getfile&r=" + Math.random(),
        data: { "id": id },
        beforeSend: function () {
        },
        success: function (data) {
            if (data.statu != "ok") {
                alert(data.msg);
            }
            else {
                window.location.href = data.res;
            }
        },
        error: function (jqXHR, textStatus, errorThrown) {
            alert(alert(textStatus));
        }
    });
}
这种的原理也跟第一种是一样的。虽然实现了隐藏下载地址,但是,同样的如果文件是图片也会出现第一中方式中提到的问题。
第三中就是通过服务器端更改Response 的Header实现文件的下载。
  1. // GET: /Downoad/   
  2.         public ActionResult Index() {  
  3.  
  4.             #region 文件下载   
  5.             var filePath = Server.MapPath("/images/Razor.pdf");//文件地址   
  6.             FileInfo fi = new FileInfo(filePath);  
  7.             Response.ClearHeaders();  
  8.             Response.AppendHeader("Content-Disposition""attachment;filename="  
  9.                 //将文件名改成Guid   
  10.                 + string.Format("{0:n}{1}", System.Guid.NewGuid(), fi.Extension));  
  11.             //文件的大小   
  12.             Response.AddHeader("Content-Length", fi.Length.ToString());  
  13.             Response.AppendHeader("Last-Modified", fi.LastWriteTime.ToFileTime().ToString());  
  14.             Response.AppendHeader("Location", Request.Url.AbsoluteUri);  
  15.             //文件的类型。如:pdf文件为:"application/pdf",   
  16.             //此处为"application/unknown" 未知类型(浏览器会根据文件类型自动判断)   
  17.             Response.ContentType = "application/unknown";  
  18.             Response.WriteFile(filePath);  
  19.             #endregion   
  20.             //Response.End();   
  21.             return View();  
  22.         }  
// GET: /Downoad/
        public ActionResult Index() {

            #region 文件下载
            var filePath = Server.MapPath("/images/Razor.pdf");//文件地址
            FileInfo fi = new FileInfo(filePath);
            Response.ClearHeaders();
            Response.AppendHeader("Content-Disposition", "attachment;filename="
                //将文件名改成Guid
                + string.Format("{0:n}{1}", System.Guid.NewGuid(), fi.Extension));
            //文件的大小
            Response.AddHeader("Content-Length", fi.Length.ToString());
            Response.AppendHeader("Last-Modified", fi.LastWriteTime.ToFileTime().ToString());
            Response.AppendHeader("Location", Request.Url.AbsoluteUri);
            //文件的类型。如:pdf文件为:"application/pdf",
            //此处为"application/unknown" 未知类型(浏览器会根据文件类型自动判断)
            Response.ContentType = "application/unknown";
            Response.WriteFile(filePath);
            #endregion
            //Response.End();
            return View();
        }
运行效果:


posted on 2012-08-02 11:27  云鹏@道  阅读(484)  评论(0编辑  收藏  举报

导航