asp.net core send formfile with ajax, 我无法在控制器中归档

asp.net core send formfile with ajax, i can't file in controller

asp.net核心2.2

model.file 或另一个 file.Don 不来 file.Everytime 为空。嵌套 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();
            }

        }

个人资料模型:

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');
                                }
                            });
                    }
                });

如果你想发送一个包含文件的模型 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)
    {
        ...
    }