原生JS版和jQuery 版实现文件上传功能
2024-09-07 15:14:16
<!doctype html>
<html lang="zh">
<head>
<meta charset="utf-8">
<title>HTML5 Ajax Uploader</title>
<script src="jquery-2.1.1.min.js"></script>
</head> <body>
<p><input type="file" id="upfile"></p>
<p><input type="button" id="upJS" value="用原生JS上传"></p>
<p><input type="button" id="upJQuery" value="用jQuery上传"></p>
<script>
/*原生JS版*/
document.getElementById("upJS").onclick = function() {
/* FormData 是表单数据类 */
var fd = new FormData();
var ajax = new XMLHttpRequest();
fd.append("upload", 1);
/* 把文件添加到表单里 */
fd.append("upfile", document.getElementById("upfile").files[0]);
ajax.open("post", "test.php", true); ajax.onload = function () {
console.log(ajax.responseText);
}; ajax.send(fd); }
/* jQuery 版 */
$('#upJQuery').on('click', function() {
var fd = new FormData();
fd.append("upload", 1);
fd.append("upfile", $("#upfile").get(0).files[0]);
$.ajax({
url: "test.php",
type: "POST",
processData: false,
contentType: false,
data: fd,
success: function(d) {
console.log(d);
}
});
});
</script>
</body>
</html>
最新文章
- 【CodeVS】 p1696 奇怪的函数
- nodejs:grunt使用合并压缩的基本使用
- struts.xml什么时候加载
- PYTHON线程知识再研习F---队列同步Queue
- Oracle EBS-SQL (MRP-7):检查MRP计划运行报错原因之超大数据查询2.sql
- D - Digging(01背包,贪心)
- 插入排序的性能测试对比(C与C++实现)
- python函数式编程之装饰器(一)
- odoo中各视图写法
- 简单介绍Excel单元格行列指示的实现原理(俗称聚光灯功能)
- 重温IO
- [BZOJ 3709] Bohater
- vue---import的几种表现形式
- java 泛型 ? 和 T的区别
- python-opencv 图像二值化,自适应阈值处理
- 【转】全Javascript的Web开发架构:MEAN和Yeoman【译】
- jQuery之noConflict() 方法
- matlab直方图均衡
- [MVC4]Data Annotations Extensions:无法使用EmailAddress等验证特性的解决方法
- 解决kylin build cube第一步报错:java.lang.NullPointerException
热门文章
- [ZPG TEST 108] Antimonotonicity【贪心】
- 51nod 1138 连续整数的和
- Frequency of String CodeForces - 963D
- 配置Ubuntu16.04第02步:更改镜像源,更新系统
- Sping Boot返回Json格式自定义
- 工作记录:JS正则表达式 angularjs ng-if ng-show ng-switch
- AJPFX关于static总结
- Android 重定向 init.rc中服务的输出
- CSS——BFC
- GridSearchCV 与 RandomizedSearchCV 调参