Solve problems ajax asynchronous file uploads

HTML:
<form id=" avater" name=" formlist">
{{ csrf_field() }}
<ul class="list-group container" style="width:50%">
    <li class="list-group-item list" style="border-radius: 5px">
        <input id=" image" class="file" type="file" name=" file">
        <button id=" btn" style=" width: 50px;height: 25px;padding:0" class="btn btn-primary radius" type="button">上传</button>
    </li>
</ul>
</form>
Jquery:
$('#btn').on('click', function() {
    var file = $('#image')[0].files[0];
    was form = $ ( '# avater') [0];
    There formdat = new formdat (forms);
    formdata.append('image', file)
   $.ajax({
       type: 'post',
       url: "{{url('setava')}}",
       data: formdata,
       dataType: 'json',
       processData: false,
       contentType: false,
       success: function(res) {
            if (res.status == 0) {
                 alert(res.msg);
                 window.location.href = "{{url('set')}}";
            }
      }
});
Red part of the important part, the object can be solved using FormData ajax asynchronous upload files. And simultaneously merge upload transmission form content and files, access the code behind the normal value on it. (Pick a file when the file)

Guess you like

Origin www.cnblogs.com/CWJDD/p/11456883.html