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 关注
- 128 浏览
添加回答
举报