【ajax】 html js jquery ajax上传文件【一眼就会】【实用】

先看效果:

代码:

<form id="fileUpload" action="" method="post" enctype="multipart/form-data">
    <input type="file" name="file" id="file">
    <button type="submit">上传</button>
</form>

 

 $('#fileUpload').on('submit', function (e) {
        e.preventDefault(); // 不自动提交

        var formData = new FormData(this);
        formData.append('xxx', xxx);//自定义参数。
        formData.append('xxx', xxx);
        formData.append('xxx', xxx);
        formData.append('xxx', xxx);
        formData.append('xxx', xxx);
        formData.append('xxx', xxx);

        $.ajax({
            url: 'https://www.liuguiqing.com:8080/fileupload', 
            type: 'POST',
            data: formData,
            contentType: false, // 不管类型
            processData: false, // 不管数据
            success: function (response) {
                console.log(response);
            },
            error: function (error) {
                console.error(error);
            }
        });
    });

 

posted @ 2024-08-29 18:35  刘贵庆  阅读(1)  评论(0编辑  收藏  举报