1 回答

TA貢獻(xiàn)1874條經(jīng)驗(yàn) 獲得超12個(gè)贊
當(dāng)您上傳文件時(shí),您將無(wú)法使用,
application/x-www-form-urlencoded
您必須使用multipart/form-data
你不應(yīng)該將字符串與 formData 混合,
send('upload='+formData)
它只會(huì)導(dǎo)致你上傳等于upload=[Object object]
您應(yīng)該只發(fā)送 formData 并讓 XHR 或 Fetch 自動(dòng)為您處理內(nèi)容類型。
如果您想要一個(gè)數(shù)組,那么我想您也想要該屬性
mulitple
?為了更好的衡量,你總是可以添加required
和accept="image/*, .txt"
如果您只使用接受表單元素的第一個(gè)構(gòu)造函數(shù)參數(shù),則無(wú)需手動(dòng)將所有文件添加到表單數(shù)據(jù)中,表單中的所有內(nèi)容都將被添加
<form method="POST" action="https://httpbin.org/post" id="quoteform" encoding="multipart/form-data">
<input multiple type="file" name="files[]" id="quote" required />
<input type="submit" value="upload"/>
</form>
<script>
// html (xml) should mark the settings, behavior as it mostly always have done
// in all years way back and the js should provide enhancement and
// be more generally written to enhance the site (if js where
// to be turned off - most unlikely, I for once have it disabled in my phone)
// static sites works better without ads + annoying cookie popups
// it's a good thing Brave have quick toggle to enable scrips for js-only pages
function ajaxify (evt) {
evt.preventDefault()
var form = evt.target
var formData = new FormData(form)
fetch(form.action, {
method: form.method,
body: formData
}).then(res => res.text()).then(alert)
}
document.getElementById("quoteform").addEventListener("submit", ajaxify)
</script>
- 1 回答
- 0 關(guān)注
- 199 瀏覽
添加回答
舉報(bào)