1 回答

TA貢獻1874條經驗 獲得超12個贊
當您上傳文件時,您將無法使用,
application/x-www-form-urlencoded
您必須使用multipart/form-data
你不應該將字符串與 formData 混合,
send('upload='+formData)
它只會導致你上傳等于upload=[Object object]
您應該只發送 formData 并讓 XHR 或 Fetch 自動為您處理內容類型。
如果您想要一個數組,那么我想您也想要該屬性
mulitple
?為了更好的衡量,你總是可以添加required
和accept="image/*, .txt"
如果您只使用接受表單元素的第一個構造函數參數,則無需手動將所有文件添加到表單數據中,表單中的所有內容都將被添加
<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 關注
- 216 瀏覽
添加回答
舉報