Mr.Kiyak
Mr.Kiyak

Reputation: 31

Plupload Asp.Net MVC how pass different parameters into ActionResult method

I use Plupload for Asp.Net MVC

I need to pass any value or viewmodel into the ActionResult method

For example first line code of form

@using (Html.BeginForm("UploadImage", "Home",
            FormMethod.Post, new { enctype = "multipart/form-data" }))

ActionResult method signature

[HttpPost]
public ActionResult UploadImage(int? chunk, string name, **I NEED PASS PARAMETER HERE !!!**)

I couldn't.

Upvotes: 3

Views: 2017

Answers (4)

Pure.Krome
Pure.Krome

Reputation: 86987

First, have a quick look at the official documentation.

There's the following api property I quickly found:

multipart_params

Object name/value collection with arguments to get posted together with the multipart file.

So let's look at some sample code:

var uploader = new plupload.Uploader({
    runtimes : 'html5,html4',
    browse_button : 'pickfiles',
    max_file_size : '10mb',
    resize : {width : 320, height : 240, quality : 90},
    url : 'upload.php',
    filters : [
        {title : "Image files", extensions : "jpg,gif,png"},
        {title : "Zip files", extensions : "zip"}
    ],
        multipart_params: { param1: 'value1', something: $('#foo').val() }
});

and your action method...

[HttpPost]
public ActionResult UploadImage(int? chunk, int chunks, string name, string param1, string something)
{ ... }

Try that .. (that's untested .. but enough to get you started)

Upvotes: 3

RasikaSam
RasikaSam

Reputation: 5573

If you need to pass different parameters for each file thats been uploaded, worth having a look at Passing dynamic parameters with each file in plupload It is working with my MVC4 project.

Upvotes: 5

Manfred
Manfred

Reputation: 5666

@Pure.Krome's solution works if you have static values that don't change as users enter data into the page. I used that solution until I ran into the problem that the content of elements may have changed. Therefore I needed a solution that would modify the multipart_params only just before when the upload starts.

If multipart_params are passed to the contructor of Uploader() then using $('#foo').val() uses the value of the element with id 'foo' that is has at that time. If element 'foo' is a form element then this may not be what you want.

Therefore, here is an alternative. For the constructor you may want to pass the following parameters:

var uploader = new plupload.Uploader({
   // other params
   multipart: true
});

Then just before you start the upload you need to set the multipart_params. For example you might have a button somewhere on your page. For that button you have a JavaScript handler that starts the upload. The resulting handler including setting the multipart_params then might look as follows:

$('#uploadfiles').click(function (e) {
   uploader.settings.multipart_params = { param1: document.getElementById("id1").value, param2: document.getElementById("id2").value };
   uploader.start();
   e.preventDefault();
});

Note that you can change the name 'param1' to something more meaningful e.g. 'Title'. You probably also will have an id more meaningful than 'id' for your input elements. On the controller side (I'm using MVC 4), the implementation then may look something like:

public ActionResult Upload(string name = "", int chunk = 0, int chunks = 0, string param1 = "", string param2 = "") {
   // ... your code here ...
}

To stay with the example: If you changed the parameter name from 'param1' to 'title' then of course the parameter name for the action also needs to be changed from 'param1' to 'title' accordingly. The resulting code, also showing where you get the file stream from looks then as follows:

public ActionResult Upload(string name = "", int chunk = 0, int chunks = 0, string title = "", string param2 = "") {
   // ... your code here ...
   System.Web.HttpPostedFileBase fileUpload = Request.Files[0];
   // ... and more of your code here ...
}

Upvotes: 2

Mohamed Farrag
Mohamed Farrag

Reputation: 1692

As far as I understand , I think you need to pass HttpPostedFile as third parameter

Upvotes: 0

Related Questions