最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

原生JS和jQuery版實(shí)現(xiàn)文件上傳功能

 更新時(shí)間:2016年04月18日 15:19:33   投稿:lijiao  
這篇文章主要介紹了HTML5中用js、jQuery結(jié)合Ajax實(shí)現(xiàn)文件上傳功能,HTML5中已經(jīng)可以用Ajax上傳文件了,而且代碼簡單,借助 FormData類即可發(fā)送文件數(shù)據(jù),感興趣的小伙伴們可以參考一下

本文實(shí)例分享了原生JS版和jQuery 版實(shí)現(xiàn)文件上傳功能的例子,供大家參考,具體內(nèi)容如下

<!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 是表單數(shù)據(jù)類 */
 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>

php代碼:

<?php
if (isset($_POST['upload'])) { 
var_dump($_FILES);
move_uploaded_file($_FILES['upfile']['tmp_name'], 'up_tmp/'.time().'.dat');
//header('location: test.php');
exit;
}
?>

更多精彩內(nèi)容請參考專題《ajax上傳技術(shù)匯總》,《javascript文件上傳操作匯總》《jQuery上傳操作匯總》進(jìn)行學(xué)習(xí)。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。

相關(guān)文章

最新評論

丹寨县| 莱州市| 凌云县| 三台县| 台山市| 阳高县| 康保县| 吉安县| 澄城县| 丹阳市| 竹溪县| 渝北区| 陆良县| 呼图壁县| 阿拉尔市| 霍林郭勒市| 巩义市| 城口县| 罗平县| 泸州市| 双鸭山市| 和田市| 吴堡县| 广南县| 宜章县| 六枝特区| 虞城县| 增城市| 宝坻区| 息烽县| 临猗县| 盐池县| 罗定市| 清苑县| 浦县| 偏关县| 建始县| 双辽市| 四川省| 阿克| 大余县|