flask使用ajax上传图片或者文件

        function upload_cover(){
            var cover = new FormData();
            var fileObj = document.getElementById('cover').files[0];
            cover.append('cover', fileObj)
            $.ajax({
                type: 'post',
                url: '/upload_cover',
                data: cover,
                async: false,
                processData: false,
                contentType: false,
                success: function (data, status) {
                    if (data==''){
                        alert('上传封面失败');
                    } else{
                        var cover_url = data['cover'];

                    }
                },
            });
        }

processData必须设置为false否则ajax会报错

flask后端依旧使用request.files获取文件信息

原文地址:https://www.cnblogs.com/lgh344902118/p/8694903.html