需要一份ASP.NET MVC3 上传文件的代码。
具体要求就是有二个表,一个Article,内有id,title,content字段,一个Attachment,内有id,attid,attpath字段。现在就是要在添加文...
具体要求就是有二个表,一个Article,内有id,title,content字段,一个Attachment,内有id,attid,attpath字段。
现在就是要在添加文章时,可以上传多个文件,将文件路径保存到Attachment表。在内容页面可以显示这些上传的文件。
需要这样的一个增删改查代码。用EF 展开
现在就是要在添加文章时,可以上传多个文件,将文件路径保存到Attachment表。在内容页面可以显示这些上传的文件。
需要这样的一个增删改查代码。用EF 展开
展开全部
View:
@{
ViewBag.Title = "DownloadAdd";
}
<html>
<head>
<title>后台</title>
</head>
<body>
<div id="body-wrapper">
@RenderPage("../Shared/BackLeft.cshtml")
<form action="DownloadAdd" method="post" enctype="multipart/form-data">
<div id="main-content">
<!-- Main Content Section with everything -->
<noscript>
<!-- Show a notification if the user has disabled javascript -->
<div class="notification error png_bg">
<div>
Javascript is disabled or is not supported by your browser. Please <a href="http://browsehappy.com/"
title="Upgrade to a better browser">upgrade</a> your browser or <a href="http://www.google.com/support/bin/answer.py?answer=23852"
title="Enable Javascript in your browser">enable</a> Javascript to navigate
the interface properly. Download From <a href="http://www.exet.tk">exet.tk</a></div>
</div>
</noscript>
<div class="content-box">
<!-- Start Content Box -->
<div class="content-box-header">
<h3>
下载</h3>
@* <ul class="content-box-tabs">
<li><a href="#tab1" class="default-tab">分类</a></li>
<!-- href must be unique and match the id of target div -->
<li><a href="#tab2">Forms</a></li>
</ul>*@
<div class="clear">
</div>
</div>
<!-- End .content-box-header -->
<div class="content-box-content">
<div class="tab-content default-tab" id="tab1">
<!-- This is the target div. id must match the href of this div's tab -->
<div class="notification attention png_bg">
<a href="#" class="close">
<img src="@Url.Content("~/Content/Images/icons/cross_grey_small.png")" title="Close this notification" alt="close" /></a>
<div>
添加内容时注意格式
</div>
</div>
@Html.Label("文件名")
<input type="text" name="Name" />
@Html.Label("顺序")
<input type="text" name="OrderNum" />
@Html.Label("分类")
<input type="text" name="Category"/>
@Html.Label("上传")
<input type="file" name="fileData" />
<input type="submit" value="确定" />
</div>
<!-- End #tab1 -->
</div>
<!-- End .content-box-content -->
</div>
</div>
</form>
</div>
</body>
</html>
Controller:
public ActionResult DownloadAdd()
{
ActionResult a = LoginSuccess();
if (a != null)
{
return a;
}
return View();
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult DownloadAdd(FormCollection form)
{
DownloadDAO downloadDao = new DownloadDAO();
Download download = new Download();
//if (Request.Files.Count == 0)
//{
// return MessageBack("请选择下载文件!");
//}
if(Request.Files.Count!=0)
{
if(Request.Files[0].FileName.Length==0)
{
return MessageBack("请选择下载文件");
}
}
string category = form["Category"].Trim();
if (category.Length > 100 || category.Length == 0)
{
return MessageBack("分类不合法!");
}
download.Statu = category;
string name = form["Name"].Trim();
if (name.Length > 50 || name.Length == 0)
{
return MessageBack("名字不合法!");
}
download.DName = name;
int orderNum;
if (Int32.TryParse(form["OrderNum"].Trim(), out orderNum) == true)
{
Download d = downloadDao.GetOneByOrder(orderNum);
if (d == null)
{
download.OrderNum = orderNum;
}
else
{
return MessageBack("已存在此序号!");
}
}
else
{
return MessageBack("序号不合法!");
}
HttpPostedFileBase file = Request.Files[0];
DateTime date = DateTime.Now;
//时间串
string f = date.ToString("yyyyMMddHHmmssff");
//原文件名
string str = file.FileName;
//正式文件名
string filename = f + str;
//所在目录
string destination = Server.MapPath("~");
//完整地址(相对)
string path = destination +"/Upload/"+ filename;
file.SaveAs(path);
string sqlpath = "/Upload/" + filename;
download.DUrl = sqlpath;
if (downloadDao.AddOne(download) == true)
{
return MessageRedirect("添加成功!", "DownloadManager");
}
else
{
return MessageBack("添加失败,请检查数据!");
}
}
DAO:
/// <summary>
/// 添加下载信息
/// </summary>
/// <param name="download"></param>
/// <returns></returns>
public bool AddOne(Download download)
{
try
{
DBHelper.Entity.AddToDownloads(download);
int count = DBHelper.Entity.SaveChanges();
return true;
}
catch (Exception)
{
return false;
}
}
@{
ViewBag.Title = "DownloadAdd";
}
<html>
<head>
<title>后台</title>
</head>
<body>
<div id="body-wrapper">
@RenderPage("../Shared/BackLeft.cshtml")
<form action="DownloadAdd" method="post" enctype="multipart/form-data">
<div id="main-content">
<!-- Main Content Section with everything -->
<noscript>
<!-- Show a notification if the user has disabled javascript -->
<div class="notification error png_bg">
<div>
Javascript is disabled or is not supported by your browser. Please <a href="http://browsehappy.com/"
title="Upgrade to a better browser">upgrade</a> your browser or <a href="http://www.google.com/support/bin/answer.py?answer=23852"
title="Enable Javascript in your browser">enable</a> Javascript to navigate
the interface properly. Download From <a href="http://www.exet.tk">exet.tk</a></div>
</div>
</noscript>
<div class="content-box">
<!-- Start Content Box -->
<div class="content-box-header">
<h3>
下载</h3>
@* <ul class="content-box-tabs">
<li><a href="#tab1" class="default-tab">分类</a></li>
<!-- href must be unique and match the id of target div -->
<li><a href="#tab2">Forms</a></li>
</ul>*@
<div class="clear">
</div>
</div>
<!-- End .content-box-header -->
<div class="content-box-content">
<div class="tab-content default-tab" id="tab1">
<!-- This is the target div. id must match the href of this div's tab -->
<div class="notification attention png_bg">
<a href="#" class="close">
<img src="@Url.Content("~/Content/Images/icons/cross_grey_small.png")" title="Close this notification" alt="close" /></a>
<div>
添加内容时注意格式
</div>
</div>
@Html.Label("文件名")
<input type="text" name="Name" />
@Html.Label("顺序")
<input type="text" name="OrderNum" />
@Html.Label("分类")
<input type="text" name="Category"/>
@Html.Label("上传")
<input type="file" name="fileData" />
<input type="submit" value="确定" />
</div>
<!-- End #tab1 -->
</div>
<!-- End .content-box-content -->
</div>
</div>
</form>
</div>
</body>
</html>
Controller:
public ActionResult DownloadAdd()
{
ActionResult a = LoginSuccess();
if (a != null)
{
return a;
}
return View();
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult DownloadAdd(FormCollection form)
{
DownloadDAO downloadDao = new DownloadDAO();
Download download = new Download();
//if (Request.Files.Count == 0)
//{
// return MessageBack("请选择下载文件!");
//}
if(Request.Files.Count!=0)
{
if(Request.Files[0].FileName.Length==0)
{
return MessageBack("请选择下载文件");
}
}
string category = form["Category"].Trim();
if (category.Length > 100 || category.Length == 0)
{
return MessageBack("分类不合法!");
}
download.Statu = category;
string name = form["Name"].Trim();
if (name.Length > 50 || name.Length == 0)
{
return MessageBack("名字不合法!");
}
download.DName = name;
int orderNum;
if (Int32.TryParse(form["OrderNum"].Trim(), out orderNum) == true)
{
Download d = downloadDao.GetOneByOrder(orderNum);
if (d == null)
{
download.OrderNum = orderNum;
}
else
{
return MessageBack("已存在此序号!");
}
}
else
{
return MessageBack("序号不合法!");
}
HttpPostedFileBase file = Request.Files[0];
DateTime date = DateTime.Now;
//时间串
string f = date.ToString("yyyyMMddHHmmssff");
//原文件名
string str = file.FileName;
//正式文件名
string filename = f + str;
//所在目录
string destination = Server.MapPath("~");
//完整地址(相对)
string path = destination +"/Upload/"+ filename;
file.SaveAs(path);
string sqlpath = "/Upload/" + filename;
download.DUrl = sqlpath;
if (downloadDao.AddOne(download) == true)
{
return MessageRedirect("添加成功!", "DownloadManager");
}
else
{
return MessageBack("添加失败,请检查数据!");
}
}
DAO:
/// <summary>
/// 添加下载信息
/// </summary>
/// <param name="download"></param>
/// <returns></returns>
public bool AddOne(Download download)
{
try
{
DBHelper.Entity.AddToDownloads(download);
int count = DBHelper.Entity.SaveChanges();
return true;
}
catch (Exception)
{
return false;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询