admin管理员组文章数量:1393375
Context
I have a page with questions, this has an structure like the following
sections:[{
section: 1,
questions:[{
question: 1,
attachment: [FormData Object]
...
},
{
question: 2,
attachment: [FormData Object]
...
}]
}, ...]
Each question have an attachment. What I did was to create a FormData
object, upload the file into it and add it to the question object. Example:
let formData = new FormData();
formData.append('file', event.target.files[0])
question.attachment = formData
Everything's fine so far. The problem es when I try to send it to the server. I doesn't pass the form object to it. This is the code I use to send it:
this.$http.post('my-path', {data: this.sections}, {headers: {'Content-Type': 'multipart/form-data'}, emulateJSON: true})
Using emulateJSON: true
sends the data but attachment
attribute is not contained in the request.
Using headers: {'Content-Type': 'multipart/form-data'}
for some reason send a null request.
Is there a way to do something like that and actually works?
Context
I have a page with questions, this has an structure like the following
sections:[{
section: 1,
questions:[{
question: 1,
attachment: [FormData Object]
...
},
{
question: 2,
attachment: [FormData Object]
...
}]
}, ...]
Each question have an attachment. What I did was to create a FormData
object, upload the file into it and add it to the question object. Example:
let formData = new FormData();
formData.append('file', event.target.files[0])
question.attachment = formData
Everything's fine so far. The problem es when I try to send it to the server. I doesn't pass the form object to it. This is the code I use to send it:
this.$http.post('my-path', {data: this.sections}, {headers: {'Content-Type': 'multipart/form-data'}, emulateJSON: true})
Using emulateJSON: true
sends the data but attachment
attribute is not contained in the request.
Using headers: {'Content-Type': 'multipart/form-data'}
for some reason send a null request.
Is there a way to do something like that and actually works?
Share Improve this question edited Jun 27, 2018 at 12:40 Luis felipe De jesus Munoz asked Jun 27, 2018 at 12:31 Luis felipe De jesus MunozLuis felipe De jesus Munoz 7,9533 gold badges38 silver badges54 bronze badges3 Answers
Reset to default 3I think the problem resides in serialization of form object.
I suggest you to encode each file in base64 and append it instead of form data object.
Or if you prefer posting form data included in JSON. Read about the correct way of form data serialization.
In my experience encoding files in base64 was the preferred way.
you must set processdata to false
this.$http.post('my-path', {
cache: false,
processData: false,
data: this.sections,
},
{
headers: {
'Content-Type': 'multipart/form-data'
},
emulateJSON: true
};)
as @eWizard said. And he can use How to convert file to base64 in JavaScript??
I faced the problem with it but i solved it using async and await.
then in server he can convert it to file again.
and change the function to be like that:
getBase64: function(file){
var reader = new FileReader();
return new Promise((resolve, reject) => {
reader.onerror = () => {
reader.abort();
reject(new DOMException("Problem parsing input file."));
};
reader.onload = () => {
resolve(reader.result);
};
reader.readAsDataURL(file);
});
}
and use it as the following
var fileReaderPromise = await this.getBase64(image);
本文标签: javascriptUpload file in json to serverStack Overflow
版权声明:本文标题:javascript - Upload file in json to server - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744645135a2617356.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论