1

ASP.NET 核心 2.2

model.file 或其他文件。不要来文件。每次都为空。嵌套 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; }
    }

查询:

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

1 回答 1

3

如果你想使用 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)
    {
        ...
    }
于 2019-12-03T07:55:18.743 回答