jquery實現文件頁面表單提交

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
 <script language="JavaScript" src="/js/jquery-1.8.3.js" ></script>
<script type="text/javascript">
function UpladFile() {


    var fileObj = document.getElementById("file").files[0]; // 獲取文件對象

    var FileController = "http://192.168.23.174:8080/WeiKe/fileUpload?action=DruqwPmF1keVPn1s15Cy&userId=BC1000037589";                    // 接收上傳文件的後臺地址
    // FormData 對象

    var form = new FormData();

    form.append("author", "hooyes");                        // 可以增加表單數據

    form.append("file", fileObj);                           // 文件對象


    // XMLHttpRequest 對象

   var xhr = new XMLHttpRequest();

    xhr.open("post", FileController, true);

    xhr.onload = function () {

        alert("上傳完成!");

    };

    xhr.send(form);
 
}
</script>
</head>
<body>
 <input type="file" id="file" name="myfile" />

<input type="button" οnclick="UpladFile()" value="上傳" />
</body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章