这是Model层
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace 续点上传视频图片.Models
{
public class MovieModel
{
public string Id { get; set; }
public string MovieName { get; set; }
public string PhotoPath { get; set; }
public string MoviePath { get; set; }
}
}
这是分页类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace 续点上传视频图片.Models
{
/// <summary>
/// 分页
/// </summary>
public class Pager
{
public List<MovieModel> list { get; set; }
public int count { get; set; }
}
}
这是控制器
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using 续点上传视频图片.Models;
using System.Data.SqlClient;
using System.Data;
namespace 上传视屏图片MVC.Controllers
{
public class KZQController : Controller
{
// GET: KZQ
/// <summary>
/// 添加视图
/// </summary>
/// <returns></returns>
public ActionResult Index()
{
return View();
}
/// <summary>
/// 添加方法
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public int MovieAdd(MovieModel model)
{
using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=FileMovie;Integrated Security=True"))
{
conn.Open();
string str = string.Format("insert into Movie values ('{0}','{1}','{2}')", model.MovieName, model.PhotoPath, model.MoviePath);
SqlCommand com = new SqlCommand(str, conn);
return com.ExecuteNonQuery();
}
}
/// <summary>
/// 展示视图
/// </summary>
/// <returns></returns>
public ActionResult Show()
{
return View();
}
/// <summary>
/// 展示方法
/// </summary>
/// <param name="pageindex"></param>
/// <param name="pagesize"></param>
/// <returns></returns>
public string MovieShow(int pageindex,int pagesize)
{
using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=FileMovie;Integrated Security=True"))
{
conn.Open();
string sql = string.Format("select * from movie");
DataTable dt = new DataTable(sql);
SqlDataAdapter ada = new SqlDataAdapter(sql,conn);
ada.Fill(dt);
string json = JsonConvert.SerializeObject(dt);
var list = JsonConvert.DeserializeObject<List<MovieModel>>(json);
Pager pp = new Pager();
pp.list = list.Skip((pageindex - 1) * pagesize).Take(pagesize).ToList();
pp.count = list.Count;
string json1 = JsonConvert.SerializeObject(pp);
return json1;
}
}
public int MovieDel(int id)
{
using (SqlConnection conn=new SqlConnection("Data Source=.;Initial Catalog=FileMovie;Integrated Security=True"))
{
conn.Open();
string sql = string.Format("delete from movie where Id={0}",id);
SqlCommand cmd = new SqlCommand(sql, conn);
return cmd.ExecuteNonQuery();
}
}
/// <summary>
/// 文件上传
/// </summary>
/// <returns></returns>
[HttpPost]
public JsonResult Plupload(string filename2)//自己加的filename2
{
string[] dd = Request.Headers.AllKeys;
string msg = string.Empty;
int chunk = Convert.ToInt32(Request["chunk"]); //当前分块
int chunks = Convert.ToInt32(Request["chunks"]);//总的分块数量
long hcouns = 0;
string lujing = null;//自己加的
foreach (string upload in Request.Files)
{
if (upload != null && upload.Trim() != "")
{
string path = AppDomain.CurrentDomain.BaseDirectory + "Temp\\";
if (!Directory.Exists(path)) //判断给定的路径上是否存在该目录
{
Directory.CreateDirectory(path); //不存在则创建该目录
}
var postedFile = Request.Files[upload]; //获取客户端上载文件的集合
string filename = filename2.Substring(0, filename2.LastIndexOf('.')); //获取客户端上传文件的名称及后缀
string hou = Path.GetExtension(filename2);//自己加的
string newFileName = filename;
if (chunks > 1)
{
newFileName = chunk + "_" + filename; //按文件块重命名块文件
}
string fileNamePath = path + newFileName; //将块文件和临时文件夹路径绑定
if (chunks > 0)
{
for (int i = 0; i < chunks; i++)
{
//检测已存在磁盘的文件区块
if (!System.IO.File.Exists(path + i.ToString() + "_" + filename) && i != chunk)
{
hcouns = i * postedFile.ContentLength;
break;
}
}
}
if (chunks == 1) //自己加的
{
lujing = DateTime.Now.ToFileTime() + hou;
postedFile.SaveAs(path + lujing); //保存上载文件内容
}
else
postedFile.SaveAs(fileNamePath);
if (chunks > 1 && chunk + 1 == chunks) //判断块总数大于1 并且当前分块+1==块总数(指示是否为最后一个分块)
{
lujing = DateTime.Now.ToFileTime() + hou;//自己加的
using (FileStream fsw = new FileStream(path + lujing, FileMode.Create, FileAccess.Write))
{
BinaryWriter bw = new BinaryWriter(fsw);
// 遍历文件合并
for (int i = 0; i < chunks; i++)
{
bw.Write(System.IO.File.ReadAllBytes(path + i.ToString() + "_" + filename)); //打开一个文件读取流信息,将其写入新文件
System.IO.File.Delete(path + i.ToString() + "_" + filename); //删除指定文件信息
bw.Flush(); //清理缓冲区
}
}
}
}
}
//var json =Json(new { str ="/Temp\\"+filename2, jsonrpc = "2.0", result = "", id = "id", hcount = "" + hcouns.ToString() + "" });
return Json("/Temp/" + lujing);//自己加的
}
[HttpPost]
public string Checkplupload()
{
string fileName = Request["fileName"].ToString();
//文件名称
long Size = Request["size"] == null ? 0 : Convert.ToInt64(Request["size"]);
//文件的分块大小
int fileCount = Request["maxFileCount"] == null ? 0 : Convert.ToInt32(Request["maxFileCount"]);
//文件一共的分块数量。比如1G以20M分块,则有50块。
//上面变量通过加载文件的时候通过ajax方式提交过来
long hcouns = 0;
string path = AppDomain.CurrentDomain.BaseDirectory + "Temp\\";
if (fileCount > 0)
{
for (int i = 0; i < fileCount; i++)
{
//检测已存在磁盘的文件区块
if (!System.IO.File.Exists(path + i.ToString() + "_" + fileName))
{
//你懂的,如果服务器上不存在如 i_文件名这个文件,那证明客户端应该从这个字节开始往服务器上传。
hcouns = i * Size;
//服务器已存在区块的总字节数
break;
}
}
}
//返回到客户端
//return Json(new { result = hcouns });
string json = JsonConvert.SerializeObject(new { result = hcouns });
return json;
}
}
}
这是添加页面
需要拉脚本Component
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<script src="~/Scripts/jquery-1.10.2.js"></script>
<link href="~/Content/bootstrap.css" rel="stylesheet" />
<script type="text/javascript" src="@Url.Content("~/Content/Component/plupload_2_1_2/jquery.js")" charset="UTF-8"></script>
<script type="text/javascript" src="@Url.Content("~/Content/Component/plupload_2_1_2/pB.js")" charset="UTF-8"></script>
<script type="text/javascript" src="@Url.Content("~/Content/Component/plupload_2_1_2/jquery-ui.min.js")" charset="UTF-8"></script>
<script type="text/javascript" src="@Url.Content("~/Content/Component/plupload_2_1_2/plupload.full.min.js")" charset="UTF-8"></script>
<script type="text/javascript" src="@Url.Content("~/Content/Component/plupload_2_1_2/jquery.ui.plupload/jquery.ui.plupload.js")" charset="UTF-8"></script>
<script type="text/javascript" src="@Url.Content("~/Content/Component/plupload_2_1_2/zh_CN.js")" charset="UTF-8"></script>
<link type="text/css" rel="stylesheet" href="@Url.Content("~/Content/Component/plupload_2_1_2/jquery-ui.min.css")" media="screen" />
<link type="text/css" rel="stylesheet" href="@Url.Content("~/Content/Component/plupload_2_1_2/jquery.ui.plupload/css/jquery.ui.plupload.css")" media="screen" />
<style type="text/css">
.plupload_logo {
background-image: none;
}
</style>
</head>
<body>
<div id="uploader" style="height:300px">
<p>你的浏览器不支持HTML5,Flash,或silverlight,请升级浏览器或联系管理员</p>
</div>
<div style="margin:50px auto;width:50%">
<label><h1>内容描述</h1></label>
<table>
<tr>
<td>文件名称:</td>
<td><input id="movieName" type="text" /><input id="photoPath" type="hidden" /><input id="moviePath" type="hidden" /></td>
</tr>
</table>
<input type="button" name="name" onclick="add()" value="添加到列表" />
<input type="button" name="name" onclick="fh()" value="展示页面" />
</div>
</body>
</html>
<script>
function fh() {
location.href = "/KZQ/Show";
}
function add() {
if (($("#movieName").val() == "") && ($("#photoPath").val() == "")) {
alert("请先上传视频或者照片");
}
//else if ($("#movieName").val() == "") {
// alert("必须输入文件名称");
//}
else {
$.ajax({
url: '/KZQ/MovieAdd',
type: 'post',
data: { MovieName: $("#movieName").val(), PhotoPath: $("#photoPath").val(), MoviePath: $("#moviePath").val() },
dataType: 'json',
success: function (a) {
if (a > 0) {
alert("添加成功");
}
else {
alert("有Bug");
}
}
})
}
}
</script>
<script type="text/javascript">
// 初始化
$(function () {
$("#uploader").plupload({
init: {
BeforeUpload: function (up, file) {
// Called right before the upload for a given file starts, can be used to cancel it if required
up.settings.multipart_params = {
filename2: file.name
};
},
FileUploaded: function (up, file, result) {
var str = result.response.split('.');
console.log(str[1]);
if (str[1] == 'jpg"' || str[1] == 'gif"' || str[1] == 'png"' || str[1] == 'rmvb"')
$("#photoPath").val(result.response);
else
$("#moviePath").val(result.response);
}
},
runtimes: 'html5,flash,silverlight,html4', // 这里是说用什么技术引擎
url: '@Url.Content("/KZQ/Plupload")', // 服务端上传路径
max_file_size: '6097mb', // 文件上传最大限制。
max_file_count: 2, //指示用户可以同时上传文件的最大数量
chunk_size: '1mb', // 上传分块每块的大小,这个值小于服务器最大上传限制的值即可。
multipart: true,
// 是否生成缩略图(仅对图片文件有效)
resize: {
width: 200,
height: 200,
quality: 90,
crop: true // crop to exact dimensions
},
// 这个数组是选择器,就是上传文件时限制的上传文件类型
filters: [
{ title: "Image files", extensions: "jpg,gif,png,mp4,rmvb,txt" },
{ title: "Zip files", extensions: "zip,avi,rar" }
],
rename: true, // 是否重命名文件
sortable: true, // Sort files
dragdrop: false, //启用文件到小部件能够拖放(操作)(目前唯一HTML5支持)
// Views to activate
views: {
list: true,
thumbs: true, // Show thumbs
active: 'thumbs'
},
// plupload.flash.swf 的所在路径
flash_swf_url: '@Url.Content("~/Content/Component/plupload_2_1_2/Moxie.swf")',
// silverlight所在路径
silverlight_xap_url: '@Url.Content("~/Content/Component/plupload_2_1_2/Moxie.xap")'
});
});
</script>
这是展示页面
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Show</title>
<script src="~/Scripts/jquery-1.10.2.js"></script>
<link href="~/Content/bootstrap.css" rel="stylesheet" />
</head>
<body>
<div>
<input type="button" onclick="fh()" name="name" value="返回添加" />
</div>
<div id="table">
</div>
<nav>
<div id="pagelist">
</div>
</nav>
</body>
</html>
<script>
function fh()
{
location.href = "/KZQ/Index";
}
$(function () {
pager(1, 3);
});
function pager(pageindex, pagesize) {
$.ajax({
url: "/KZQ/MovieShow",
type: "get",
dataType: "json",
data: { pageindex: pageindex, pagesize: pagesize },
success: function (a) {
$("#table").empty();
var str = "<table class='table'>";
str += "<tr>";
str += "<td>编号</td>";
str += "<td>名称</td>";
str += "<td>图片</td>";
str += "<td>电影</td>";
str += "<td>操作</td>";
str += "</tr>";
for (var i = 0; i < a.list.length; i++) {
str += "<tr>";
str += "<td>" + a.list[i].Id + "</td>";
str += "<td>" + a.list[i].MovieName + "</td>";
str += "<td><img src=" + a.list[i].PhotoPath + " style='width:150px;height:150px'/></td>";
str += "<td><video controls='controls' style='height:150px;width:150px;'><source src=" + a.list[i].MoviePath + " type='video/mp4'/></video><td>";
str += "<td><input type='button' onclick='del("+a.list[i].Id+")' value='删除'/></td>";
str += "<tr>";
}
str += "</table>";
$("#table").append(str);
PageList(a.count, pageindex)
}
})
}
function del(id)
{
if (confirm("确定要删除吗?"))
{
$.ajax({
url: "/KZQ/MovieDel",
data: { Id: id },
type: "post",
dataType: "json",
success: function (a) {
if (a > 0) {
alert("删除成功");
location.reload();
}
else {
alert("删除失败");
}
}
})
}
}
function PageList(count,pageindex)
{
$("#pagelist").empty();//清空分页导航
var pagesum = Math.ceil(count / 3);//计算总页数
var str = "";
str += "总共" + pagesum + "页";
str += " ";
if(pageindex>1)
{
str += "<a onclick='pager(1,3)'>首页</a>";
str += "<a onclick='pager(" + (pageindex - 1) + ",3)'>上一页</a>";
}
else
{
str += "首页";
str += "上一页";
}
if(pageindex<pagesum)
{
str += "<a onclick='pager(" + (pageindex + 1) + ",3)'>下一页</a>";
str += "<a onclick='pager(" + pagesum + ",3)'>尾页</a>";
}
else
{
str += "下一页";
str += "尾页";
}
str += "当前第" + pageindex + "页";
$("#pagelist").append(str);
}
</script>