【发布时间】:2020-01-06 11:19:03
【问题描述】:
首先,我检查了file-upload-in-vuetify 和vuetify-file-uploads 的问题。但是那里的解决方案不起作用。
我正在尝试使用 vuetify 2 <v-file-input> 发送多个 PDF 文件。我创建了FormData 对象并附加了我的所有文件,但是当我尝试提交时,它没有到达我的后端。我只是得到一个空对象。这是我的代码:
模板:
<v-layout>
<v-flex>
<v-file-input show-size counter chips multiple label="Arquivo Geral" ref="myfile" v-model="files"></v-file-input>
</v-flex>
<v-flex>
<v-btn color="primary" text @click="submitFiles">test</v-btn>
</v-flex>
</v-layout>
脚本:
data() {
return {
files: null,
}
}
methods: {
submitFiles(){
let formData = new FormData()
if(this.files){
for( let file in this.files){
formData.append("cave", file)
}
console.log(formData.getAll("cave"))
console.log(this.files)
axios.post('https://eniwp6w65oc77.x.pipedream.net/',
{
files: formData,
test: "test"
},
{
headers: {
'Content-Type': 'multipart/form-data'
}
}).then( response => {
console.log('Success!')
console.log({response})
}).catch(error => {
console.log({error})
})
}
else {
console.log('there are no files.')
}
}
}
requestbin 中的响应正文和标头:
正文:
{
"files": {},
"test": "test"
}
标题:
host: eniwp6w65oc77.x.pipedream.net
Accept: application/json, text/plain, */*
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9,pt;q=0.8,gl;q=0.7
Cache-Control: no-cache
Content-Type: multipart/form-data
Origin: http://localhost:8000
Pragma: no-cache
Referer: http://localhost:8000/
Sec-Fetch-Mode: cors
Sec-Fetch-Site: cross-site
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36
Content-Length: 28
Connection: keep-alive
【问题讨论】:
-
你能展示你的后端吗
-
嗨@MarcelusTrojahn,主要问题是我的formData对象,在“文件”键中,在我的requestbin链接中为空。我不知道是什么原因造成的。我怀疑问题可能出在请求中或我如何制作我的 formData 对象。客户端的请求是我在“脚本”代码中的 axios 发布请求。我在服务器端添加了缺少的请求标头。
-
@ßiansorÅ.Ålmerol 我一直在使用 requestbin 来测试我的回复。我现在更新了标题和正文
标签: javascript vue.js axios vuetify.js