62,046
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
namespace MvcApplication1.Controllers
{
public class AdminController : Controller
{
//
// GET: /Admin/
public FileContentResult Images(string name)
{
Response.Write(name);
string imagepath = "/admin/images/" + name;
imagepath = Server.MapPath(imagepath);
//根据图片文件的路径使用文件流打开,并保存为byte[]
FileStream fs = new FileStream(imagepath, FileMode.Open);
byte[] byData = new byte[fs.Length];
fs.Read(byData, 0, byData.Length);
fs.Close();
Response.Clear();
return new FileContentResult(byData, "image/jpeg");
}
}
}
routes.MapRoute(
"ImageRoute", // 路由名称
"ReWriteUrl/{name}", // 带有参数的 URL
new { controller = "Admin", action = "Images", name = UrlParameter.Optional } // 参数默认值
, new { name = @"[\w]+\.(jpg|gif|png)" }
);