Use jquery to upload image files (for easy understanding, image verification is omitted)

//上传图片,获取url
            $("#file").change(function (){
                if (!$(this).val()){return}
                var fileData = new FormData();
                fileData.append("file", $('#file')[0].files[0]);
                console.log($('#file')[0].files[0])
                $.ajax({
                    url:IMG_UPLOAD_URL,
                    type: "POST",
                    contentType: false,//核心,如果为true的时候,jq就会去操作这个分界符,导致后台服务器无法操作数据,导致请求失败。
                    processData: false,//核心,默认为true,规定通过请求发送的数据是否转换为查询字符串,
                    data:fileData,
                    success(res){
                        //图片预览
                        $("#titleImg").attr("src",JSON.parse(res.data).img_url);
                    }
                })
            })

Guess you like

Origin blog.csdn.net/weixin_43158695/article/details/112844955