分类“C#/Asp.Net”下的文章

asp.net mvc 简易上传功能

1、上传图片页面 VIEW


<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>upload</title>
<link href="../../Content/admin/css/style.css" rel="stylesheet" type="text/css">
<style type="text/css">
.style1
{
height: 25px;
}
</style>
</head>
<body bgcolor="#FFFFFF">
<table width="96%" border="0" cellspacing="1" cellpadding="3" align=center>
<tr>
<th colspan=2 align=left>文件要求:<br /></th>
</tr>
<tr>
<td colspan=2 align=left>
允许上传(<font color=red><%=ViewData["uptype"] %></font>)格式的文件<br />
文件大小:不超过<%=ViewData["filemaxsize"]%></th>
</tr>
<form action="<%=Url.Action("Process") %>" enctype="multipart/form-data" method="post">
<tr>
<th colspan=2 align=left>选择文件:<br /></th>
</tr>
<tr>
<td width="100%" colspan=2><input name="up1" type="file" />&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" name="Submit" value="上传"> </td>
</tr>
</table>
</body>
</html>

2、Controlles

#region 上传初始化
public ActionResult Upload(int? id)
{
String filemaxsize = System.Convert.ToInt32(ConfigurationSettings.AppSettings["filemaxsize"])/1024+"KB";
String uptype = "";
if(id==1){
uptype = "htm,html,shtml";
}else if(id==2){
uptype = "jpg,png,gif,bpm";
}else if(id==3){
uptype="swf";
}else if(id==4){
uptype = "wmv,avi,wma,mp3,mid";
}
Session["uptype"] = id;
ViewData["uptype"] = uptype;
ViewData["filemaxsize"] = filemaxsize;
return View();
}
#endregion

#region 处理上传程序
public ActionResult Process(HttpPostedFileBase up1)
{
String filepath = ConfigurationSettings.AppSettings["filepath"];
int filemaxsize=System.Convert.ToInt32(ConfigurationSettings.AppSettings["filemaxsize"]);
string str = Server.MapPath(filepath);
String path = "";
String id = Session["uptype"].ToString();
String uptype = "";
String fodder = "";

if (id.Equals("1"))
{
uptype = "htm,html,shtml";
path = filepath + "Link/";
fodder = "Link";
}
else if (id.Equals("2"))
{
uptype = "jpg,png,gif,bpm";
path = filepath + "Img/";
fodder = "Img";
}
else if (id.Equals("3"))
{
uptype = "swf";
path = filepath + "Flash/";
fodder = "Flash";
}
else if (id.Equals("4"))
{
uptype = "wmv,avi,wma,mp3,mid";
path = filepath + "Media/";
fodder = "Media";
}
//------------判断文件夹是否存在:start--------
if (!Directory.Exists(str + fodder))
{
Directory.CreateDirectory(str + fodder);
}
//------------判断文件夹是否存在:end--------
String fileapaths = up1.FileName;
String[] pathstr = fileapaths.Split(new Char[] { '\\' });
String filename = pathstr[pathstr.Length-1];
//-------------验证后缀:start-----------------
String[] files = filename.Split(new Char[] { '.' });
String filepostfix = files[files.Length-1];
if (!Tools.ISCheck(filepostfix, uptype))
{
return Content(Tools.ResponseErrorHtml("不支持的上传类型:" + filepostfix + ",<a href='/Admin/Upload/" + id + "'>返回</a>"));
}
//-------------验证后缀:end-------------------
//-------------验证文件大小:start-------------
int filesize=up1.ContentLength;
if (filesize > filemaxsize)
{
return Content(Tools.ResponseErrorHtml("文件超过(<font color=red>"+(filemaxsize/1024)+"KB"+"</font>)限制,<a href='/Admin/Upload/" + id + "'>返回</a>"));
}
//-------------验证文件大小:end---------------
//------------重命名文件:start----------------
DateTime dt = DateTime.Now;
string timestr = dt.Year+""+dt.Month+""+dt.Day+""+dt.Hour+""+dt.Minute+""+dt.Second+""+dt.Millisecond+""; //这是最直接的转化方法
// filename = timestr + Tools.Convert(filename);     //方式1:日期+原文件名
filename = timestr +"."+ filepostfix;               //方式2:日期
//------------重命名文件:end------------------
path = path.Replace("/","\\");
up1.SaveAs(Server.MapPath(path + filename));
Session["uptype"] = "";
String finshpath = path + filename;
finshpath = finshpath.Replace("\\","/");
return Content(Tools.ResponseErrorHtml("<script>function upload(){callback('" + finshpath + "');}</script>上传成功:<font color=red>" + finshpath + "</font>"
+"!&nbsp;&nbsp;&nbsp;&nbsp;<a href='javascript:void(0);' onclick='upload();'><font color=red>确定完成<font></a>"));
}
#endregion

ASP.NET-MVC-权限系统的设计及疑难解决

思路 :后台所有的操作都在AdminControllers里面。然后通过一个Filter来过滤整个Controllers

namespace TodayeSystem.AdminControllers
{

[TodayeSystem.Controllers.ActionFillters]
public class AdminController : Controller
{
//Action处理区域
}
}

ActionFillters.cs

 public class ActionFillters : FilterAttribute, IActionFilter

    {

        public void OnActionExecuting(ActionExecutingContext filterContext)

        {

            String url = new UrlHelper(filterContext.RequestContext).Action("AdminLogin", "Default");

            String actionName = filterContext.RouteData.Values["Action"].ToString();

            if (actionName.Equals("Index") && actionName.Equals("Login") && actionName.Equals("Admin_Logout"))

            {

            }

            else

            {

                if (filterContext.HttpContext.Session["UserName"] != null)

                {

                    string username = filterContext.HttpContext.Session["UserName"].ToString();

                    if (username != null && !username.Equals(""))

                    {

                    }

                    else

                    {

                        filterContext.HttpContext.Response.Redirect(url, true);

                    }

                }

                else

                {

                    filterContext.HttpContext.Response.Redirect(url, false);
 

                }

            }

        }

    }

然后在Filter中限制具体每一个的权限