【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); } }); });
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步