c#斷點續傳MVC
這是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; } } }
這是新增頁面
需要拉指令碼
@{ 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>
這是展示頁面
@{ 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>