asp.net mvc实现图片下载防盗链及提示是否存在!

本文介绍如何在ASP.NET MVC中使用自定义路由处理图片请求,实现图片防盗链及无图提示功能。通过配置路由规则,使服务器能够根据请求路径加载对应图片或提示图片,并通过控制器方法动态返回图片资源。

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

首先声明此方法对服务器影响很大:
目的:用file/图片名称代替files/图片名称访问,自定义无图片提示和防盗链

在gobal.ascx中添加规则:

 routes.MapRoute(
                
"Image",
                
"file/{fileName}",
                
new { controller = "file", action = "producePics" },
                
new { fileName = @"^(\w+).(\w+)$" }
                );

创建images文件夹新建两张图片.nopic.png和notallowlink.png
把图片文件放在Files文件夹下(该例子中放了6张图片),等下我们用file/图片名称代替files/图片名称访问

创建File controller并创建Index action

        public ActionResult Index()
        {
            DirectoryInfo dir 
= new DirectoryInfo(Server.MapPath("~/files/"));
            FileInfo[] files
=dir.GetFiles();

            ViewData[
"dirinfo"]="该文件夹下共有"+files.Length+"个文件";
            
foreach (FileInfo file in files)
            {
                ViewData[
"imagelist"+= "<li><a href=\"/file/" + file.Name+ "\"><img src=\"" +
                    "/file/" + file.Name + "\"/></a></li>";
            }
            
return View();
        }

创建 Index view

ContractedBlock.gifExpandedBlockStart.gifCode
    <html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>Index</title>
    
<style>
        #imagelist
{margin:5px;padding:0px;list-style:none;}
        #imagelist li
{border:solid 1px lightskyblue;padding:1px;margin:3px 4px;
                      float
:left;}
        #imagelist li img
{width:200px;height:155px;}
    
</style>
</head>
<body>
    
<div>
     > 
<%=ViewData["dirinfo"%><br />
     
<ul id="imagelist">
        
<%=ViewData["imagelist"%>
     
</ul>
    
</div>
</body>
</html>

扩展FileController


   
using System.IO;
   
    
public class FileController : Controller
    {
        
public FileResult ProducePic(string fileName)
        {
            
//00:00:00.0039062  最差的
             /*
         FileStream fs=new FileStream(Server.MapPath("~/files/"+fileName),FileMode.Open,FileAccess.Read);
             byte[] buffer=new byte[fs.Length];
             fs.Read(buffer, 0, buffer.Length);
             fs.Flush();
             fs.Close();
             return File(buffer, "Image/JPEG");
*/

            
//00:00:00.0029297 (刷新几次发现偶尔为00:00:00.00009611)
            
//FileStream fs = new FileStream(Server.MapPath("~/files/" + fileName),FileMode.Open,FileAccess.Read);
            
//return File(fs, "Image/JPEG");

            
//00:00:00.0029296 处理最快的方法 (多刷新几次发现基本为00:00:00.00000011)
            
//return File(Server.MapPath("~/files/") + fileName, "Image/JPEG");


            
//如果不存在图片则显示无图提示的图片
            /*
            FileInfo file = new FileInfo(Server.MapPath("~/files/") + fileName);
            if (!file.Exists) file = new FileInfo(Server.MapPath("~/files/") + "nopic.jpg");
            FileStream fs = new FileStream(file.FullName, FileMode.Open, FileAccess.Read);
            FileResult files = File(fs, "Image/JPEG");
            fs.Flush();
            return files; 
*/


            
string filePath = null;
            
bool isAllowLink=Request.Url.Host == "localhost";               //是否允许外部链接
            if (!isAllowLink)
            {
                filePath 
= Server.MapPath("~/images/"+ "notallowlink.png";
            }
            
else
            {
                filePath 
= Server.MapPath("~/files/"+ fileName;           //获取图片地址
                if (!System.IO.File.Exists(filePath))                       //图片不存在的话显示提示的图片
                    filePath = Server.MapPath("~/images/" + "nopic.png");
            }
            
return File(filePath, "Image/GIF");
        }
    }

实现结果如下:


http://www.cnyolee.com

转载于:https://www.cnblogs.com/newmin/archive/2009/09/01/asp_net_mvc_pic_keepstoplink.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值