导航:首页 > 文件教程 > ajax提交form表单文件上传

ajax提交form表单文件上传

发布时间:2025-03-20 16:58:52

1. 使用jquery.form.js实现文件上传及进度条前端代码

ajax的表单提交只能提交data数据到后台,没法实现file文件的上传还有展示进度功能,这里用到form.js的插件来实现,搭配css样式简单易上手,而且高大上,推荐使用。

需要解释下我的结构, #upload-input-file 的input标签是真实的文件上传按钮,包裹form标签后可以实现上传功能, #upload-input-btn 的button标签是展示给用户的按钮,因为需要样式的美化。上传完成生成的文件名将会显示在 .upload-file-result 里面, .progress 是进度条的位置,先让他隐藏加上 hidden 的class, .progress-bar 是进度条的主体, .progress-bar-status 是进度条的文本提醒。

去掉hidden的class,看到的效果是这样的
[图片上传失败...(image-2c700a-1548557865446)]

将上传事件绑定在file的input里面,绑定方式就随意了。
var progress = $(".progress-bar"), status = $(".progress-bar-status"), percentVal = '0%'; //上传步骤 $("#myupload").ajaxSubmit({ url: uploadUrl, type: "POST", dataType: 'json', beforeSend: function () { $(".progress").removeClass("hidden"); progress.width(percentVal); status.html(percentVal); }, uploadProgress: function (event, position, total, percentComplete) { percentVal = percentComplete + '%'; progress.width(percentVal); status.html(percentVal); console.log(percentVal, position, total); }, success: function (result) { percentVal = '100%'; progress.width(percentVal); status.html(percentVal); //获取上传文件信息 uploadFileResult.push(result); // console.log(uploadFileResult); $(".upload-file-result").html(result.name); $("#upload-input-file").val(''); }, error: function (XMLHttpRequest, textStatus, errorThrown) { console.log(errorThrown); $(".upload-file-result").empty(); } });

[图片上传失败...(image-3d6ae0-1548557865446)]

[图片上传失败...(image-9f0adf-1548557865446)]

更多用法可以 参考官网

2. ajax怎么提交带文件上传表单

上传的文件是没有办法和表单内容一起异步的,可考虑使用jquery的ajaxfileupload,或是其他的插件,异步上传文件后,然后再对表单进行操作。

3. ajax怎样提交form表单与实现文件上传

Ajax 提交form方式可以来将form表单序列化源 然后将数据通过data提交至后台,例如:

阅读全文

与ajax提交form表单文件上传相关的资料

热点内容
怎么用键盘拷贝u盘文件 浏览:664
js炫酷特效 浏览:69
什么样的app最有市场 浏览:563
腾讯地图如何截图工具 浏览:69
数据处理主要方法有哪些 浏览:567
ps6拖文件无法打开 浏览:668
有哪些比较好的书评app 浏览:520
java中char表示范围 浏览:154
python编程操作题如何批改 浏览:1
网络是什么原理图 浏览:670
apk音乐提取工具 浏览:89
怎样共享wifi密码设置 浏览:281
javamesdk8 浏览:503
属于声音格式的文件有哪些 浏览:373
汉字书法的app有哪些 浏览:801
迅捷fwr310v1升级 浏览:782
三星有密码锁可以关机 浏览:293
游戏编程设计怎么做 浏览:509
脸谱网账号密码大全 浏览:98
编程猫什么时候才能登录 浏览:600

友情链接