This is a migrated thread and some comments may be shown as answers.

Sync Upload Not Receiving Files at Controller

2 Answers 151 Views
Upload
This is a migrated thread and some comments may be shown as answers.
Lonnie
Top achievements
Rank 1
Lonnie asked on 13 Feb 2014, 02:59 PM
I am using Kendo Upload but without the MVC wrappers. I am unable to get the files at the controller to save. 

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "fileID,galleryItemID,width,height")] GalleryItemFile galleryFile, IEnumerable<HttpPostedFileBase> uplFile)
        {
 
            if (ModelState.IsValid)
            {
                GalleryItemFile thisGalleryFile = db.GalleryItemFiles.Find(galleryFile.fileID);
                if (uplFile != null)
                {
                    foreach (var inputFile in uplFile)
                    {
                        //HttpPostedFileBase uploadedFile = Request.Files[inputFile];
 
                        if (inputFile != null && inputFile.ContentLength > 0)
                        {
                            string filename = galleryFile.galleryItem.imageName + DateTime.Now.ToString("yyyyMMdd-HHmm-ss");
 
                            filename = filename.Replace(" ", "-") + "." + Path.GetExtension(inputFile.FileName);
                            string filepath = Path.Combine(Server.MapPath("~/Areas/Gallery/Content/GalleryFiles"), filename);
                            inputFile.SaveAs(filepath);
 
                            galleryFile.filename = filename;
                            galleryFile.filepath = Path.Combine("~/Areas/Gallery/Content/GalleryFiles", filename);
                        }
                    }
                }

@model Marketing.Areas.Gallery.Models.GalleryItemFile
 
@using (Ajax.BeginForm("Edit", new { id = Model.fileID }, new AjaxOptions { UpdateTargetId = "slideoutmenu", InsertionMode = InsertionMode.Replace, HttpMethod = "Post" }, new { enctype = "multipart/form-data" }))
    {
    @Html.AntiForgeryToken()
    @Html.HiddenFor(model => model.galleryItem.galleryItemID)
    @Html.HiddenFor(model => model.fileID)
 
 
        <table class="formtable">
            <tr><td colspan="2" class="tableheader">Edit Gallery File Item</td></tr>
             
            <tr>
                <td>Upload File:</td>
                <td><input type="file" name="uplFile" id="uplFile" /></td>
            </tr>
            <tr>
                <td>Item Name:</td>
                <td>@Html.EditorFor(model => model.width)</td>
            </tr>
            <tr>
                <td>Description:</td>
                <td>@Html.EditorFor(model => model.height)</td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit" value="Save" class="btn btn-default" /> <input type="submit" value="Cancel" class="btn btn-cancel" onclick="slideOutMenuClose(); $('#slideoutmenu').html('');" /></td>
            </tr>
        </table>
        <script>
            $("#uplFile").kendoUpload();
        </script>
}

What am I doing wrong. I've tried this multiple ways and nothing seems to get the file. 

2 Answers, 1 is accepted

Sort by
0
Lonnie
Top achievements
Rank 1
answered on 13 Feb 2014, 03:10 PM
Sorry I should add that if never gets into the if (uplFile != null) statement so uplFile is null.
0
Petur Subev
Telerik team
answered on 17 Feb 2014, 01:18 PM
Hello Lonnie,

Consider using regular form, because the Ajax form does not support sending files. Same discussed here:

http://stackoverflow.com/questions/2491230/binding-httppostedfilebase-using-ajax-beginform

Basically you can do the opposite, you can send the other input fields with the request of the Ajax upload. If interested you can check the article here:

http://docs.telerik.com/kendo-ui/getting-started/using-kendo-with/aspnet-mvc/helpers/upload/metadata#using-the-upload-client-side-event

Kind Regards,
Petur Subev
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
Upload
Asked by
Lonnie
Top achievements
Rank 1
Answers by
Lonnie
Top achievements
Rank 1
Petur Subev
Telerik team
Share this question
or