Mandar Patil
Mandar Patil

Reputation: 538

Async file upload in MVC using kendoUpload

I am using file uploader with MVC.

Following is my code :

<div class="demo-section k-content">
<input name="files" id="files" type="file" />
</div>


<script>
$(document).ready(function () {
            var data = JSON.stringify({
            'ReportID': '@(Model.ReportID)',
        });

    $("#files").kendoUpload({
        async: {
            saveUrl: '@Url.Action("save", "UserPage")',

            //removeUrl: "remove",
            autoUpload: true,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: data,
        }//,
    });
});

on ActionResult I am using following code :

string fileName = Path.GetFileName(files.FileName);
fileName = model.ReportID + "s" + Guid.NewGuid() + extension;

Everything is working fine except the value of model.ReportID its returning NULL every time.

I am missing something here?

Upvotes: 0

Views: 2276

Answers (2)

pavemann
pavemann

Reputation: 384

Try something like that:

 @(Html.Kendo().Upload()
        .Name("uploadFiles")
        .Async(a => a
            .Save("Save", "Upload")
            .Remove("Remove", "Upload")
            .AutoUpload(true)
            .SaveField("files")
            //.Batch(true)
            .RemoveField("fileNames")
        )
        .Multiple(true)
        .ShowFileList(true)
        .Events(events => events
        .Error("onUploadError")
        .Progress("onUploadProgress")
        .Complete("onUploadComplete")
        .Success("onUploadSuccess")
        .Select("onUploadSelect")
        .Upload("onUploadAdditionalData")
        .Remove("onUploadRemove"))
    )

inside the onUploadAdditionalData event you can pass parameters like:

function onUploadAdditionalData(e) {
e.data = { val1: val1, val2: val2 };

}

your controller action should look like this:

 public ActionResult Save(IEnumerable<HttpPostedFileBase> files, string val1, string val2)
    {
    //do upload handling here
    }

Upvotes: 1

Gene R
Gene R

Reputation: 3744

If you check documentation http://docs.telerik.com/kendo-ui/api/javascript/ui/upload#configuration-async async.data is undocumented and i am not sure if there is such property.

You can put it directly to saveUrl:

saveUrl: '@Url.Action("save", "UserPage", new { ReportID = Model.ReportID })'

Upvotes: 0

Related Questions