English 中文(简体)
How to use the plupload package with ASP.NET MVC?
原标题:

I am using plupload version 1.3.0

More specifically how I have to define my controller action to support chunking? Can I use the HttpPosteFileBase as a parameter?

At the moment I am using the following code to initialize the plugin

In the HEAD tag

<link type="text/css" rel="Stylesheet" media="screen" href="<%: Url.Content( "~/_assets/css/plupload/jquery.ui.plupload.css" )%>" />
<link type="text/css" rel="Stylesheet" media="screen" href="<%: Url.Content( "~/_assets/css/plupload/gsl.plupload.css" )%>" />
<script type="text/javascript" src="<%: Url.Content( "~/_assets/js/plupload/gears_init.js" )%>"></script>
<script type="text/javascript" src="<%: Url.Content( "~/_assets/js/plupload/plupload.full.min.js" )%>"></script>
<script type="text/javascript" src="<%: Url.Content( "~/_assets/js/plupload/jquery.ui.plupload.min.js" )%>"></script>

On document ready

$("#uploader").pluploadQueue({
    runtimes:  html5,html4,gears,flash,silverlight ,
    url:  <%: Url.Content( "~/Document/Upload" ) %> ,
    max_file_size:  5mb ,
    chunk_size:  1mb ,
    unique_names: true,
    filters: [
        { title: "Documenti e Immagini", extensions: "doc,docx,xls,xlsx,pdf,jpg,png" }
    ],
    multiple_queues: false
});
最佳回答

Here you go:

[HttpPost]
public ActionResult Upload(int? chunk, string name)
{
    var fileUpload = Request.Files[0];
    var uploadPath = Server.MapPath("~/App_Data");
    chunk = chunk ?? 0;
    using (var fs = new FileStream(Path.Combine(uploadPath, name), chunk == 0 ? FileMode.Create : FileMode.Append))
    {
        var buffer = new byte[fileUpload.InputStream.Length];
        fileUpload.InputStream.Read(buffer, 0, buffer.Length);
        fs.Write(buffer, 0, buffer.Length);
    }
    return Content("chunk uploaded", "text/plain");
}

This method will be called multiple times for each chunk and for each file being uploaded. It will pass as parameter the chunk size and the filename. I am not sure as to whether you could use a HttpPostedFileBase as action parameter because the name is not deterministic.

问题回答

Look here:

$("#uploader").pluploadQueue({
         // General settings
         runtimes:  silverlight ,
         url:  /Home/Upload ,
         max_file_size:  10mb ,
         chunk_size:  1mb ,
         unique_names: true,
         multiple_queues: false,

         // Resize images on clientside if we can
         resize: { width: 320, height: 240, quality: 90 },

         // Specify what files to browse for
         filters: [
            { title: "Image files", extensions: "jpg,gif,png" },
            { title: "Zip files", extensions: "zip" }
        ],

         // Silverlight settings
         silverlight_xap_url:  ../../../Scripts/upload/plupload.silverlight.xap 
      });

      // Client side form validation
      $( form ).submit(function (e) {
         var uploader = $( #uploader ).pluploadQueue();

         // Files in queue upload them first
         if (uploader.files.length > 0) {
            // When all files are uploaded submit form
            uploader.bind( StateChanged , function () {
               if (uploader.files.length === (uploader.total.uploaded + uploader.total.failed)) {
                  $( form )[0].submit();
               }
            });

            uploader.start();
         } else {
            alert( You must queue at least one file. );
         }

         return false;
      });

And in Controller:

[HttpPost]
public string Upload(  ) {
          HttpPostedFileBase FileData = Request.Files[0];

          if ( FileData.ContentLength > 0 ) {
             var fileName = Path.GetFileName( FileData.FileName );
             var path = Path.Combine( Server.MapPath( "~/Content" ), fileName );
             FileData.SaveAs( path );
          }

          return "Files was uploaded successfully!";
       }

That s all...No chunk is needed in Controller...





相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签