ASP.NET MVC 4 – Session + Plupload – UI Widget ( NuGet ) 方法

ASP.NET MVC 4 – Session + Plupload – UI Widget ( NuGet ) 方法

   /Controllers/PluploadController.cs
 
   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Web;
   using System.Web.Mvc;
 
   namespace CRMPortal.Controllers
   {
      public class PluploadController : Controller
      {
 
         … …
 
         public ActionResult PhotoForm()
         {
            return View();
         }
 
         public ActionResult PhotoUpload(FormCollection collection, HttpPostedFileBase file)
         {
            List<String> list;
 
            if(collection != null && file != null){
 
               if(System.Web.HttpContext.Current.Session["_SessionFile"] != null)
                  list = System.Web.HttpContext.Current.Session["_SessionFile"] as List<String>;
               else
                  list = new List<string>();
 
                list.Add(collection["name"]);
 
               file.SaveAs(Server.MapPath("~/Images/plupload/") + file.FileName);
               System.Web.HttpContext.Current.Session["_SessionFile"] = list;
            }
 
            return View();
         }
 
         public ActionResult PhotoResult()
         {
            List<String> list = System.Web.HttpContext.Current.Session["_SessionFile"] as List<String>;
            System.Web.HttpContext.Current.Session["_SessionFile"] = null;
 
            return View(list);
         }
 
         … …
 
      }
   }
 
   /Views/Plupload/PhotoForm.cshtml
 
   <link type="text/css" rel="stylesheet" href="/Content/themes/base/jquery-ui.css" media="screen" />
   <link type="text/css" rel="stylesheet" href="/Scripts/plupload/jquery.ui.plupload/css/jquery.ui.plupload.css" media="screen" />
 
   <script type="text/javascript" src="/Scripts/jquery-1.8.2.min.js" charset="UTF-8"></script>
   <script type="text/javascript" src="/Scripts/jquery-ui-1.8.24.min.js" charset="UTF-8"></script>
   <script type="text/javascript" src="/Scripts/plupload/plupload.full.min.js" charset="UTF-8"></script>
   <script type="text/javascript" src="/Scripts/plupload/jquery.ui.plupload/jquery.ui.plupload.min.js" charset="UTF-8"></script>
 
   <div id="uploader">
      <p>Your browser doesn’t have Flash, Silverlight or HTML5 support.</p>
   </div>
 
   <script type="text/javascript">
 
      $(function () {
         $("#uploader").plupload({
            runtimes: "html5,flash,silverlight,html4",
            url: "/Plupload/PhotoUpload",
            max_file_size: "2mb",
            chunk_size: "1mb",
            resize: {
               width: 50,
               height: 50,
               quality: 100,
               crop: true
            },
            filters: [
               { title: "Image files", extensions: "jpg,gif,png" },
               { title: "Zip files", extensions: "zip,avi" }
            ],
            rename: true,
            sortable: true,
            dragdrop: true,
            views: {
               list: true,
               thumbs: true,
               active: "thumbs"
            },
            init: {
               UploadComplete: function () {
                  window.location = "@(Url.Action("PhotoResult"))";
               }
            },
            flash_swf_url: "/Scripts/plupload/Moxie.swf",
            silverlight_xap_url: "/Scripts/plupload/Moxie.xap"
         });
      });
 
   </script>
 
   /Views/Plupload/PhotoUpload.cshtml
 

 

   /Views/Plupload/PhotoResult.cshtml
 
   @model List<String>
   @{
      ViewBag.Title = "PhotoResult";
   }
 
   <h2>Photo Result</h2>
 
   @foreach(String item in Model){
      <div>@(item)</div>
   }