我正在尝试上传文件使用网络获取api和PHP。但由于某些原因,这些文件没有被捕获(或发送?)从web浏览器到服务器。我仍然可以用curl发送文件。
async function send_files(url, files) { // files = [File(), File(), ...], url = localhost
const formData = new FormData()
for (const file of files) {
formData.append('files[]', file)
}
var result = await fetch(url, {
method: 'POST',
cors: 'same-origin',
headers: {
'Content-Type': 'multipart/form-data'
},
body: formData
})
return await result.json()
}
而且在
菲律宾比索
如果我
print_r($_FILES)
它返回一个空数组。现在我知道上传是这样的:
curl -k -X POST -c ... -b ... -H "Content-Type: multipart/form-data" -F "files[]=@file1" -F "files[]=@file2" ... https://localhost
“我的网站”的“网络”选项卡中的请求负载显示:
------WebKitFormBoundaryXXXXXXXXXXXXXXXX
Content-Disposition: form-data; name="files[]"; filename="file1"
Content-Type: text/plain
------WebKitFormBoundaryXXXXXXXXXXXXXXXX
Content-Disposition: form-data; name="files[]"; filename="file2"
Content-Type: text/plain
------WebKitFormBoundaryXXXXXXXXXXXXXXXX--
networking选项卡还显示我只向服务器发送了372 B的信息,两个文件的总大小为941 B。无论是在前端还是后端,我都没有收到任何错误。