首先声明此方法对服务器影响很大:
目的:用file/图片名称代替files/图片名称访问,自定义无图片提示和防盗链
Code
using System.IO;
![](https://www.cnblogs.com/Images/dot.gif)
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");
}
}
目的:用file/图片名称代替files/图片名称访问,自定义无图片提示和防盗链
在gobal.ascx中添加规则:
routes.MapRoute(
"Image",
"file/{fileName}",
new { controller = "file", action = "producePics" },
new { fileName = @"^(\w+).(\w+)$" }
);
"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();
}
{
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
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
扩展FileController
using System.IO;
![](https://www.cnblogs.com/Images/dot.gif)
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