ASP.NET Mvc Preview 5 演示Demo #3 实现MVC批量上传文件
MVC演示Demo系列: http://mike108mvp.cnblogs.com
Demo #1 实现CRUD增删查改:http://www.cnblogs.com/mike108mvp/archive/2008/08/31/1280689.html
Demo #2 实现Membership角色权限控制:http://www.cnblogs.com/mike108mvp/archive/2008/09/03/1283432.html
ASP.NET MVC QQ交流群:1215279 欢迎对 ASP.NET MVC 感兴趣的朋友加入
这个演示项目实现了在ASP.NET MVC 中批量上传多个文件的功能。【ok】
开发环境:Visual Studio 2008 SP1 + ASP.NET MVC Preview 5 + Linq To Sql + SQL Server 2005
ASP.NET MVC Preview 5 下载地址: http://www.codeplex.com/aspnet/Release/ProjectReleases.aspx?ReleaseId=16775
【Step By Step 操作步骤】
1、SQL Server 2005 执行下面的Sql查询脚本。该Sql脚本将生成一个新的mike108mvp数据库。
注意:这个数据库脚本与Demo #2 的脚本完全一样,如果你已经执行过Demo #2的数据库脚本,则可以跳过这一步。【ok】
Code
/**//****** 删除后生成数据库 ******/
USE [master]
GO
IF EXISTS (SELECT name FROM sys.databases WHERE name = N'mike108mvp')
DROP DATABASE [mike108mvp]
CREATE DATABASE [mike108mvp]
GO
/**//****** 生成表结构 ******/
USE [mike108mvp]
GO
/**//****** 对象: Table [dbo].[User] 脚本日期: 09/03/2008 20:59:55 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[User](
[UserId] [int] IDENTITY(1,1) NOT NULL,
[UserName] [nvarchar](50) NULL,
[TypeId] [int] NULL,
[Sex] [bit] NULL,
[Age] [int] NULL,
[Career] [nvarchar](50) NULL,
[FileName] [nvarchar](200) NULL,
CONSTRAINT [PK_User] PRIMARY KEY CLUSTERED
(
[UserId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Id' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'User', @level2type=N'COLUMN',@level2name=N'UserId'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'姓名' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'User', @level2type=N'COLUMN',@level2name=N'UserName'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'分类' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'User', @level2type=N'COLUMN',@level2name=N'TypeId'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'性别' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'User', @level2type=N'COLUMN',@level2name=N'Sex'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'年龄' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'User', @level2type=N'COLUMN',@level2name=N'Age'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'职业' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'User', @level2type=N'COLUMN',@level2name=N'Career'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'附件名' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'User', @level2type=N'COLUMN',@level2name=N'FileName'
2、下载本演示源码:https://files.cnblogs.com/mike108mvp/Mike108Demo03.rar
Visual Studio 2008 双击打开下载的演示源码中的 Mike108Demo.sln 解决方案文件,按F5运行。【ok】
3、UsersController.cs 输入以下代码:【ok】
namespace Mike108Demo.Controllers
{
public class MikeUploadFile
{
public string Name { get; set; }
public int Length { get; set; }
}
public class UsersController : Controller
{
Mike108mvpDataContext db = new Mike108mvpDataContext();
Demo #1 CRUD操作#region Demo #1 CRUD操作
public ActionResult UserAdd()
{
return View(@"Add");
}
public ActionResult UserCreate()
{
if (Request.HttpMethod != "POST")
{ return View(@"Add"); }
User model = new User();
UpdateModel(model, Request.Form.AllKeys);
db.Users.InsertOnSubmit(model);
db.SubmitChanges();
return RedirectToAction("UserList");
}
public ActionResult UserEdit(int userId)
{
User model = db.Users.FirstOrDefault(e => e.UserId == userId);
return View(@"Edit", model);
}
public ActionResult UserUpdate(int userId)
{
if (Request.HttpMethod != "POST")
{ return RedirectToAction("UserEdit", new { userId = userId }); }
User model = db.Users.FirstOrDefault(e => e.UserId == userId);
UpdateModel(model, Request.Form.AllKeys);
db.SubmitChanges();
return RedirectToAction("UserList");
}
public ActionResult UserDetail(int userId)
{
User model = db.Users.FirstOrDefault(e => e.UserId == userId);
return View(@"Detail", model);
}
public ActionResult UserDelete(int userId)
{
User model = db.Users.FirstOrDefault(e => e.UserId == userId);
db.Users.DeleteOnSubmit(model);
db.SubmitChanges();
return RedirectToAction("UserList");
}
public ActionResult UserList()
{
if (db.Users.Count() == 0)
{
return RedirectToAction("DataReset");
}
ViewData["UserList"] = db.Users.ToList();
return View(@"List");
}
#endregion
Demo #1 数据重置#region Demo #1 数据重置
public ActionResult DataReset()
{
//批量删除
List<User> oldList = db.Users.ToList();
db.Users.DeleteAllOnSubmit(oldList);
//批量添加
List<User> newlist = new List<User>
{
new User { UserName = "孙中山", Age = 53, Career = "中国总统" },
new User { UserName = "蒋中正", Age = 36, Career = "中国总统" },
new User { UserName = "小布什", Age = 61, Career = "美国总统" },
new User { UserName = "列宁", Age = 49, Career = "邪恶苏联" },
new User { UserName = "斯大林", Age = 49, Career = "邪恶苏联" },
new User { UserName = "比尔盖茨", Age = 62, Career = "微软老大" },
new User { UserName = "Scott Guthrie", Age = 33, Career = "ASP.NET Leader" }
};
db.Users.InsertAllOnSubmit(newlist);
db.SubmitChanges();
return RedirectToAction("UserList");
}
#endregion
Demo #2 微软Membership权限#region Demo #2 微软Membership权限
[Authorize] //登陆后才能查看
public ActionResult UserMembership()
{
User model = db.Users.FirstOrDefault(e => e.UserName == "孙中山");
return View(@"Detail", model);
}
#endregion
Demo #3 实现MVC批量上传文件#region Demo #3 实现MVC批量上传文件
public ActionResult Upload()
{
return View(@"Upload");
}
public ActionResult UploadFiles()
{
var r = new List<MikeUploadFile>();
foreach (string file in Request.Files)
{
HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
if (hpf.ContentLength == 0)
continue;
string savedFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "Upload\\" , Path.GetFileName(hpf.FileName));
hpf.SaveAs(savedFileName);
r.Add(new MikeUploadFile()
{
Name = savedFileName,
Length = hpf.ContentLength
});
}
return View(@"Upload", r);
}
#endregion
}
}
4、Views/Users/Upload.aspx 【ok】
Code
<form action="/Users/uploadfiles" method="post" enctype="multipart/form-data">
<br />
<input type="file" name="file1" /> <br />
<input type="file" name="file2" /> <br />
<input type="file" name="file3" /> <br />
<input type="file" name="file4" /> <br />
<input type="file" name="file5" /> <br />
<br />
<input type="submit" name="submit" value="批量上传" />
<input type="button" onclick="window.location.href='<%=Url.Action("UserList")%>'" value="返回" />
</form>
<% if (ViewData.Model != null)
{
foreach (MikeUploadFile v in this.ViewData.Model as List<MikeUploadFile>)
{ %>
<%=String.Format("【成功上传】: {0} 【文件大小】:{1} bytes. <br/>", v.Name, v.Length)%>
<% }
}%>
5、Visual Studio 2008 按F5运行,点击右上角的“上传”链接,进行批量上传。【ok】
6、如果你想实现新用户注册、登陆、修改密码功能,请点击下面的Demo #2(实现Membership角色权限控制)帖子链接。【ok】
下载本演示源码:
https://files.cnblogs.com/mike108mvp/Mike108Demo03.rar