最新国产好看的视频,伊人天堂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)容請(qǐng)參考專題《ajax上傳技術(shù)匯總》,《javascript文件上傳操作匯總》《jQuery上傳操作匯總》進(jìn)行學(xué)習(xí)。

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

相關(guān)文章

最新評(píng)論

张家界市| 连江县| 巴里| 桦甸市| 鸡泽县| 泰宁县| 衡东县| 玉树县| 寿阳县| 惠安县| 巫山县| 什邡市| 长海县| 金山区| 蚌埠市| 孟州市| 沈丘县| 安阳县| 南雄市| 海口市| 南平市| 徐水县| 瑞丽市| 长沙市| 分宜县| 淮南市| 汽车| 张家口市| 承德市| 昭通市| 延津县| 禄劝| 页游| 贺州市| 沙河市| 田阳县| 东丽区| 广宁县| 凉城县| 巴中市| 丹阳市|