当我尝试使用js敲除功能在asp .net mvc5中上传图像时,HttpPostedFileBase为null?

时间:2019-02-23 17:30:45

标签: javascript asp.net-mvc knockout.js asp.net-ajax

我正在尝试使用敲除js在asp .net mvc 5中上传图像,并且类型为HttpPostedFileBase的paraFrame PhotoFile变为null,并且我无法上传图像,我是使用Knockout js的新工作想要有人帮助我,

这是模型,

    public int LenderId { get; set; }
    public string GenderId { get; set; }
    public string GenderName { get; set; }
    public string Email { get; set; }
    public bool IsActive { get; set; }
    public string PhotoPath { get; set; }
    public HttpPostedFileBase PhotoFile { get; set; }

这是html

        <div class="form-group">
            <span class="control-label col-md-2"><strong>Foto:</strong></span>
            <div class="col-md-10">
                <span class="btn btn-default btn-file">
                    <input type="file" class="form-control" id="PhotoUpload" name="PhotoUpload" data-bind="attr:{src: PhotoPath}" />
                </span>
            </div>
        </div>

这是js

function lenderModel(){     var lenderModel = this;

lenderModel.LenderId = ko.observable();
lenderModel.FirstName = ko.observable();
lenderModel.MiddleName = ko.observable();
lenderModel.FirstLastName = ko.observable();
lenderModel.SecondLastName = ko.observable();
lenderModel.IdentificationCard = ko.observable();
lenderModel.GenderId = ko.observable();
lenderModel.GenderName = ko.observable();
lenderModel.Email = ko.observable();
lenderModel.IsActive = ko.observable();
lenderModel.PhotoPath = ko.observable();
lenderModel.CreateBy = ko.observable();
lenderModel.CreationDate = ko.observable();
lenderModel.ModifiedBy = ko.observable();
lenderModel.ModifiedDate = ko.observable();

}

lenderViewModel.Create = function () {
    if (lenderViewModel.IsNew()) {
        $.ajax({
            url: '/AdminLenders/Create',
            data: ko.toJSON(lenderViewModel.LenderModel()),
            dataType: "json",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            async: true,
            success: function (data) {
                if (data.Success) {
                    LoadGenders();
                    LoadListLenders();
                    $('#TextboxFirstName').val('');
                    $('#TextboxMiddleName').val('');
                    $('#TextboxFirstLastName').val('');
                    $('#TextboxSecondLastName').val('');
                    $('#TextboxIdentificationCard').val('');
                    $('#TextboxEmail').val('');
                    $('#ComboBoxGenders').val('');
                    $('#TextboxIsActive').prop('checked', false);
                    swal("Prestamista creado!", "Se ha registrado correctamente!", "success")
                }
                else {
                    swal("Error creando el prestamista!", data.Data, "error")
                }
            },
            error: function (ex) {
                alert('Ocurrión un error');
            }
        });
    }
};

由于我对敲除js还是陌生的,所以我不知道如何实现它,帮助欣赏自己的贡献和帮助

1 个答案:

答案 0 :(得分:2)

我猜测了控制器上的Create方法是什么样的,并使用FormData对象提出了这个方法。

lenderViewModel.Create = function () {
    if (lenderViewModel.IsNew()) {
            var lender = lenderViewModel.LenderModel();    

        var lenderData = new FormData();

        // Build up a matching Lender
        lenderData.append("LenderId", lender.LenderId());
        lenderData.append("GenderId", lender.GenderId());
        lenderData.append("GenderName", lender.GenderName());
        lenderData.append("Email", lender.Email());
        lenderData.append("IsActive", lender.IsActive());
        lenderData.append("PhotoPath", lender.PhotoPath());

            // Get the file and add it to the Lender
        var fileUpload = $("#PhotoUpload").get(0);
        var files = fileUpload.files;
        if(files.length === 1) {
             lenderData.append("PhotoFile", files[0]);
                }

        $.ajax({
            url: '/AdminLenders/Create',
            data: lenderData,
            enctype: "multipart/form-data",
            dataType: "json",
            type: "POST",
            contentType: false,
            processData: false,
            success: function (data) {
                if (data.Success) {
                    LoadGenders();
                    LoadListLenders();
                    $('#TextboxFirstName').val('');
                    $('#TextboxMiddleName').val('');
                    $('#TextboxFirstLastName').val('');
                    $('#TextboxSecondLastName').val('');
                    $('#TextboxIdentificationCard').val('');
                    $('#TextboxEmail').val('');
                    $('#ComboBoxGenders').val('');
                    $('#TextboxIsActive').prop('checked', false);
                    swal("Prestamista creado!", "Se ha registrado correctamente!", "success")
                }
                else {
                    swal("Error creando el prestamista!", data.Data, "error")
                }
            },
            error: function (ex) {
                alert('Ocurrión un error');
            }
        });
    }
};