js檔案上傳以及表單提交
阿新 • • 發佈:2019-01-09
原理:
使用 FormData物件,將要傳送給後臺的資料 都 append該物件中,包括檔案,最後將這個物件傳送給後臺。
<head> <meta charset="utf-8"> <title>HTML5 Ajax Uploader</title> <script src="jquery.js"></script></head>
<body> <p> <input
ajax.send(fd); //傳送 ajax請求 上傳檔案
ajax.onreadystatechange = function () { if (ajax.readyState == 4 && ajax.status == 200) { //步驟五 如果能夠進到這個判斷 說明 資料 完美的回來了,並且請求的頁面是存在的 console.log(ajax.responseText);//輸入相應的內容 } }
}
/* jQuery 版 */ $('#upJQuery').on('click', function () { var fd = new FormData(); fd.append("upload", 4); fd.append("upfile", $("#upfile").get(0).files[0]); fd.append("Headers",000);
$.ajax({ url: "/messagefile", type: "POST", processData: false, contentType: false, headers:{ token:"555" }, data: fd, success: function (d) { console.log(d); } }); }); </script></body>
</html>