asp.net核心发送带有ajax的formfile,我无法在控制器中归档

时间:2019-12-02 13:54:25

标签: ajax post form-data asp.net-core-2.2 iformfile

asp.net核心2.2

model.file或其他文件。不包含文件。每次为null。嵌套的ajax或运行订单ajax。

我想更新个人资料。我能。当文件上传到工作中时,我爆炸了。

使用不同的ajax查询进行文件上传和用户更新?我应该以不同的方法期待不同的Ajax查询吗?还是在单个ajax查询中处理?

对不起,我的英语不好。

控制器:

public IActionResult UpdateProfile(ProfileModel model, IFormFile file)
        {            
            var result = userOperation.UpdateUser(model).Result;
            if (result.Succeeded)
            {
                return Ok();
            }
            else
            {
                return BadRequest();
            }

        }

ProfileModel:

public class ProfileModel
    {
        public string UserId { get; set; }
        public string username { get; set; }
        public string Name { get; set; }
        public string SurName { get; set; }
        public string ProfilePhoto { get; set; }
        public IFormFile file { get; set; }
    }

jQuery:

var isim = $('#name');
var soyisim = $('#surname');
var menu = $('#navInside');
var cikis = $('#logout');
var updateCikis = $('#exit');
var update = $('#update');
var goUpdate = $('#goUpdate');
var kaydet = $('#kaydet');
var dosya = $('input[type=file]');
var username2 = $('#username').val();
kaydet.click(function (event) {
                event.preventDefault();
                $.ajax({
                    url: '/Registered/UpdateProfile',
                    data: { Name: isim.val(), SurName: soyisim.val(), username: '@User.Identity.Name' },
                    type: 'POST',
                    success: function (result) {
                            console.log('basarili güncelleme');
                            var formData = new FormData();
                            formData.append('file', dosya[0].files[0].name);
                            console.log(dosya[0].files[0].name);
                            console.log(JSON.stringify(dosya[0].files[0]));
                            $.ajax({
                                url: '/Registered/UpdateProfile',
                                data: { Name: isim.val(), SurName: soyisim.val(), username: '@User.Identity.Name', file: formData },
                                processData: false,
                                contentType: false,
                                type: 'POST',
                                success: function(){
                                    alert('foto da gittiii');
                                }
                            });
                    }
                });

1 个答案:

答案 0 :(得分:0)

如果要使用ajax发送包含文件的模型,可以参考以下示例:

查看

<div class="row">
<div class="col-md-4">
    <form  id="myform" enctype="multipart/form-data">
        <div class="form-group">
            <label class="control-label">Name</label>
            <input id="name" name="name" class="form-control" />
        </div>
        <div class="form-group">
            <label class="control-label">SurName</label>
            <input id="surname" name="surname" class="form-control" />
        </div>
        <div class="form-group">
            <label class="control-label">ProfilePhoto</label>
            <input id="profilePhoto" name="profilePhoto" class="form-control" />
        </div>
        <div class="form-group">
            <label class="control-label">File</label>
            <input type="file" id="name" name="name" class="form-control" />
        </div>
        <div class="form-group">
            <input type="button" id="update" value="Create" class="btn btn-primary" />
        </div>
    </form>
</div>
</div>

@section Scripts
{
<script>
var dosya = $('input[type=file]');
var update = $('#update');
    update.click(function (event) {
        //stop submit the form, we will post it manually.
        event.preventDefault();

        // Get form
        var form = $('#myform')[0];

        // Create an FormData object
        var formData = new FormData(form);

        // If you want to add an extra field for the FormData
        formData.append("file", dosya[0].files[0]);
        formData.append("username", '@User.Identity.Name');
        $.ajax({
            url: '/Home/UpdateProfile',
            data: formData,
            contentType: false,
            processData: false,
            type: 'POST',
            success: function (result) {
                alert(result);
            }
        });
    });
</script>
}

控制器

    [HttpPost]
    public IActionResult UpdateProfile(ProfileModel model)
    {
        ...
    }