Ajax File Upload With Form Data Laravel 5.3

后端 未结 5 1741
迷失自我
迷失自我 2020-12-28 21:48

i want to upload a profile image of a user to the server and i\'m stuck at ajax upload of image

all my form data are posting to datab

5条回答
  •  别那么骄傲
    2020-12-28 22:25

    Try something like this:

    $('#upload').on('click', function() {
            var file_data = $('#pic').prop('files')[0];
            var form_data = new FormData();
            form_data.append('file', file_data);
    
            $.ajax({
                    url         : 'route_url',
                    dataType    : 'text',           // what to expect back from the PHP script, if anything
                    cache       : false,
                    contentType : false,
                    processData : false,
                    data        : form_data,                         
                    type        : 'post',
                    success     : function(output){
                        alert(output);              // display response from the PHP script, if any
                    }
             });
        });
    

提交回复
热议问题