Sync Upload Not Receiving Files at Controller

3 posts, 0 answers
  1. Lonnie
    Lonnie avatar
    3 posts
    Member since:
    Feb 2014

    Posted 13 Feb 2014 Link to this post

    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. Lonnie
    Lonnie avatar
    3 posts
    Member since:
    Feb 2014

    Posted 13 Feb 2014 in reply to Lonnie Link to this post

    Sorry I should add that if never gets into the if (uplFile != null) statement so uplFile is null.
  3. Kendo UI is VS 2017 Ready
  4. Petur Subev
    Admin
    Petur Subev avatar
    1882 posts

    Posted 17 Feb 2014 Link to this post

    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!
Back to Top