注:formData中的數據在控制台上的console里面是打印不出來的,只能在控制台的network里面查看到具體的發送數據和發送選項
文章出處:夢想天空
XMLHttpRequest Level 2 添加了一個新的接口——FormData。利用 FormData 對象,我們可以通過 JavaScript 用一些鍵值對來模擬一系列表單控件,我們還可以使用 XMLHttpRequest 的 send() 方法來異步的提交表單。與普通的 Ajax 相比,使用 FormData 的最大優點就是我們可以異步上傳二進制文件。
創建一個FormData對象
你可以先創建一個空的 FormData 對象,然后使用 append() 方法向該對象里添加字段,如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
var
oMyForm =
new
FormData();
oMyForm.append(
"username"
,
"Groucho"
);
oMyForm.append(
"accountnum"
, 123456);
// 數字123456被立即轉換成字符串"123456"
// fileInputElement中已經包含了用戶所選擇的文件
oMyForm.append(
"userfile"
, fileInputElement.files[0]);
var
oFileBody =
"<a id="
a
"><b id="
b
">hey!</b></a>"
;
// Blob對象包含的文件內容
var
oBlob =
new
Blob([oFileBody], { type:
"text/xml"
});
oMyForm.append(
"webmasterfile"
, oBlob);
var
oReq =
new
XMLHttpRequest();
oReq.open(
"POST"
,
"http://foo.com/submitform.php"
);
oReq.send(oMyForm);
|
注:字段 "userfile" 和 "webmasterfile" 的值都包含了一個文件。通過 FormData.append() 方法賦給字段 "accountnum" 的數字被自動轉換為字符(字段的值可以是一個 Blob 對象,File對象或者字符串,剩下其他類型的值都會被自動轉換成字符串)。
在該例子中,我們創建了一個名為 oMyForm 的 FormData 對象,該對象中包含了名為"username","accountnum","userfile" 以及 "webmasterfile" 的字段名,然后使用XMLHttpRequest的 send() 方法把這些數據發送了出去。"webmasterfile" 字段的值不是一個字符串,還是一個 Blob 對象。
使用HTML表單來初始化一個FormData對象
可以用一個已有的 form 元素來初始化 FormData 對象,只需要把這個 form 元素作為參數傳入 FormData 構造函數即可:
|
1
|
var
newFormData =
new
FormData(someFormElement);
|
例如:
|
1
2
3
4
|
var
formElement = document.getElementById(
"myFormElement"
);
var
oReq =
new
XMLHttpRequest();
oReq.open(
"POST"
,
"submitform.php"
);
oReq.send(
new
FormData(formElement));
|
你還可以在已有表單數據的基礎上,繼續添加新的鍵值對,如下:
|
1
2
3
4
|
var
formElement = document.getElementById(
"myFormElement"
);
formData =
new
FormData(formElement);
formData.append(
"serialnumber"
, serialNumber++);
oReq.send(formData);
|
你可以通過這種方式添加一些不想讓用戶編輯的固定字段,然后再發送.
使用FormData對象發送文件
你還可以使用 FormData 來發送二進制文件.首先在 HTML 中要有一個包含了文件輸入框的 form 元素:
|
1
2
3
4
5
6
7
8
9
10
|
<form enctype=
"multipart/form-data"
method=
"post"
name=
"fileinfo"
>
<label>Your email address:</label>
<input type=
"email"
autocomplete=
"on"
autofocus name=
"userid"
placeholder=
"email"
required size=
"32"
maxlength=
"64"
/><br />
<label>Custom file label:</label>
<input type=
"text"
name=
"filelabel"
size=
"12"
maxlength=
"32"
/><br />
<label>File to stash:</label>
<input type=
"file"
name=
"file"
required />
</form>
<div id=
"output"
></div>
<a href=
"javascript:sendForm()"
>Stash the file!</a>
|
然后你就可以使用下面的代碼來異步的上傳用戶所選擇的文件:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
function
sendForm() {
var
oOutput = document.getElementById(
"output"
);
var
oData =
new
FormData(document.forms.namedItem(
"fileinfo"
));
oData.append(
"CustomField"
,
"This is some extra data"
);
var
oReq =
new
XMLHttpRequest();
oReq.open(
"POST"
,
"stash.php"
,
true
);
oReq.onload =
function
(oEvent) {
if
(oReq.status == 200) {
oOutput.innerHTML =
"Uploaded!"
;
}
else
{
oOutput.innerHTML =
"Error "
+ oReq.status +
" occurred uploading your file.<br \/>"
;
}
};
oReq.send(oData);
}
|
你還可以不借助 HTML 表單,直接向 FormData 對象中添加一個 File 對象或者一個 Blob 對象:
|
1
|
data.append(
"myfile"
, myBlob);
|
如果 FormData 對象中的某個字段值是一個 Blob 對象,則在發送 HTTP 請求時,代表該 Blob 對象所包含文件的文件名的 "Content-Disposition" 請求頭的值在不同的瀏覽器下有所不同,Firefox使用了固定的字符串"blob",而 Chrome 使用了一個隨機字符串。
你還可以使用 jQuery 來發送 FormData,但必須要正確的設置相關選項:
|
1
2
3
4
5
6
7
8
9
|
var
fd =
new
FormData(document.getElementById(
"fileinfo"
));
fd.append(
"CustomField"
,
"This is some extra data"
);
$.ajax({
url:
"stash.php"
,
type:
"POST"
,
data: fd,
processData:
false
,
// 告訴jQuery不要去處理發送的數據
contentType:
false
// 告訴jQuery不要去設置Content-Type請求頭
});
|
瀏覽器兼容性
桌面端:
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | 7+ | 4.0 (2.0) | 10+ | 12+ | 5+ |
支持filename參數 |
(Yes) | 22.0 (22.0) | ? | ? | ? |
移動端:
| Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Basic support | 3.0 | ? | 4.0 (2.0) | ? | 12+ |
? |
支持filename參數 |
? | ? | 22.0 (22.0) | ? | ? | ? |
參考文獻:
