Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am working with knockout.js. i post a form using knockout to mvc controller in the form i use a file upload. when i post ViewModel every thing is post properly but the file object always null. any one help me how to pass file object in httpPostFileBase.



This is UI


XML
<script type="text/javascript" src="~/Scripts/App/AdminScript.js"></script>
<script type="text/javascript" src="~/Scripts/App/fileBindler.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        window.ko.applyBindings(new CategoryViewModel());

    });
</script>
    <div id="divEditCategory">
        <h2>Edit Category</h2>
        <div data-bind="with:currentCategory">
            <p>Category Name: <input type="text" placeholder="Category Name" class="form-control" data-bind="value:CategoryName" /></p>
            <p>Category Name: <input type="file" id="imgPath" accept="image/*" data-bind="value: ImagePath" /></p>
            <p><input type="checkbox" data-bind="value:IsActive" />Is Active</p>
        </div>
        <p><input type="button" id="sendCategory" class="btn btn-success" value="Save" data-bind="click: saveCategory" /></p>
    </div>




And this is my ViewModel
C#
function Category(categoryName, imagePath, isActive)
{
    var fd = new FormData();
    var self = this;
    self.CategoryName = ko.observable(categoryName);
    self.IsActive = ko.observable(isActive),
    self.ImagePath,  = ko.observable(imagePath),
}
function CategoryViewModel()
{
    var self = this;

    self.currentCategory = ko.observable(new Category("","", true));

    self.saveCategory = function () {
        debugger;
        var json1 = ko.toJSON(self.currentCategory);
        debugger;
        $.ajax({
            url: '/Admin/EditCategory',
            type: 'POST',
            dataType: 'json',
            data: json1,
            contentType: 'application/json; charset=utf-8',
            success: function (data) {
                var message = data.Message;
            }
        });
    }

}
Posted
Updated 12-Nov-15 4:59am
v2
Comments
Richard Deeming 12-Nov-15 10:27am    
You need to show your code. Click "Improve question" and update the question with the relevant parts of your code. Don't forget to wrap the code in <pre>...</pre> tags.
F-ES Sitecore 12-Nov-15 11:07am    
You can't upload files via jquery like that as javascript doesn't have file access in order to serialise the file. Not that familiar with knockout, but google "upload file knockout" and you'll probably find examples of how to do this.
Member 11247784 12-Nov-15 11:19am    
F-ES Sitecore please give me alternative way to solve this problem.
Member 11247784 13-Nov-15 0:59am    
this is not good Answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900