- AJAX 教程
- AJAX - 首頁
- AJAX - 什麼是 AJAX?
- AJAX - 歷史
- AJAX - 動態網站與靜態網站
- AJAX - 技術
- AJAX - 動作
- AJAX - XMLHttpRequest
- AJAX - 傳送請求
- AJAX - 請求型別
- AJAX - 處理響應
- AJAX - 處理二進位制資料
- AJAX - 提交表單
- AJAX - 檔案上傳
- AJAX - FormData 物件
- AJAX - 傳送 POST 請求
- AJAX - 傳送 PUT 請求
- AJAX - 傳送 JSON 資料
- AJAX - 傳送資料物件
- AJAX - 監控進度
- AJAX - 狀態碼
- AJAX - 應用
- AJAX - 瀏覽器相容性
- AJAX - 示例
- AJAX - 瀏覽器支援
- AJAX - XMLHttpRequest
- AJAX - 資料庫操作
- AJAX - 安全性
- AJAX - 問題
- Fetch API 基礎
- Fetch API - 基礎
- Fetch API 與 XMLHttpRequest
- Fetch API - 瀏覽器相容性
- Fetch API - 頭部資訊
- Fetch API - 請求
- Fetch API - 響應
- Fetch API - 主體資料
- Fetch API - 憑據
- Fetch API - 傳送 GET 請求
- Fetch API - 傳送 POST 請求
- Fetch API - 傳送 PUT 請求
- Fetch API - 傳送 JSON 資料
- Fetch API - 傳送資料物件
- Fetch API - 自定義請求物件
- Fetch API - 上傳檔案
- Fetch API - 處理二進位制資料
- Fetch API - 狀態碼
- Stream API 基礎
- Stream API - 基礎
- Stream API - 可讀流
- Stream API - 可寫流
- Stream API - 變換流
- Stream API - 請求物件
- Stream API - 響應主體
- Stream API - 錯誤處理
- AJAX 有用資源
- AJAX - 快速指南
- AJAX - 有用資源
- AJAX - 討論
AJAX - 檔案上傳
AJAX 提供了一種靈活的方式來建立 HTTP 請求,該請求將檔案上傳到伺服器。我們可以使用 FormData 物件在請求中傳送單個或多個檔案。讓我們透過以下示例來討論這個概念:
示例 - 上傳單個檔案
在下面的示例中,我們將使用 XMLHttpRequest 上傳單個檔案。為此,我們首先建立一個簡單的表單,其中包含一個檔案上傳按鈕和一個提交按鈕。現在,我們編寫 JavaScript 程式碼,在其中獲取表單元素並建立一個在點選上傳檔案按鈕時觸發的事件。在這個事件中,我們將上傳的檔案新增到 FormData 物件中,然後建立一個 XMLHttpRequest 物件,該物件將使用 FormData 物件將檔案傳送到伺服器並處理伺服器返回的響應。
<!DOCTYPE html>
<html>
<body>
<!-- Creating a form to upload a file-->
<form id = "myForm">
<input type="file" id="file"><br><br>
<button type="submit">Upload File</button>
</form>
<script>
document.getElementById('myForm').addEventListener('submit', function(x){
// Prevent from page refreshing
x.preventDefault();
// Select the file from the system
// Here we are going to upload one file at a time
const myFile = document.getElementById('file').files[0];
// Create a FormData to store the file
const myData = new FormData();
// Add file in the FormData
myData.append("newFiles", myFile);
// Creating XMLHttpRequest object
var myhttp = new XMLHttpRequest();
// Callback function to handle the response
myhttp.onreadystatechange = function(){
if (myhttp.readyState == 4 && myhttp.status == 200) {
console.log("File uploaded Successfully")
}
};
// Open the connection with the web server
myhttp.open("POST", "https://httpbin.org/post", true);
// Setting headers
myhttp.setRequestHeader("Content-Type", "multipart/form-data");
// Sending file to the server
myhttp.send(myData);
})
</script>
</body>
</html>
輸出
示例 - 上傳多個檔案
在下面的示例中,我們將使用 XMLHttpRequest 上傳多個檔案。在這裡,我們使用檔案型別的屬性在 DOM 中選擇系統中的兩個檔案。然後我們將輸入檔案新增到陣列中。然後我們建立一個 FormData 物件並將輸入檔案新增到該物件。然後我們建立一個 XMLHttpRequest 物件,該物件將使用 FormData 物件將檔案傳送到伺服器並處理伺服器返回的響應。
<!DOCTYPE html>
<html>
<body>
<!-- Creating a form to upload multiple files-->
<h2> Uploading Multiple files</h2>
<input type="file">
<input type="file">
<button>Submit</button>
<script>
const myButton = document.querySelector('button');
myButton.addEventListener('click', () => {
// Get all the input files in DOM with attribute type "file":
const inputFiles = document.querySelectorAll('input[type="file"]');
// Add input files in the array
const myfiles = [];
inputFiles.forEach((inputFiles) => myfiles.push(inputFiles.files[0]));
// Creating a FormData
const myformdata = new FormData();
// Append files in the FormData object
for (const [index, file] of myfiles.entries()){
// It contained reference name, file, set file name
myformdata.append(`file${index}`, file, file.name);
}
// Creating an XMLHttpRequest object
var myhttp = new XMLHttpRequest();
// Callback function
// To handle the response
myhttp.onreadystatechange = function(){
if (myhttp.readyState == 4 && myhttp.status == 200) {
console.log("File uploaded Successfully")
}
};
// Open the connection with the web server
myhttp.open("POST", "https://httpbin.org/post", true);
// Setting headers
myhttp.setRequestHeader("Content-Type", "multipart/form-data");
// Sending file to the server
myhttp.send(myformdata);
})
</script>
</body>
</html>
輸出
結論
這就是我們如何使用 XMLHttpRequest 將檔案上傳到給定 URL 的方法。在這裡,我們可以上傳任何型別的檔案,例如 jpg、pdf、word 等,並且可以一次上傳一個檔案或多個檔案。在下一篇文章中,我們將學習如何使用 XMLHttpRequest 建立 FormData 物件。
廣告