I am trying to make a request with some data and on top of that, so I decided to make my request multipart and want to send a file along that I will be uploading and then connecting to the images field I guess, how can I do that?
This topic has been created from a Discord post (1238248201275052063) to give it more visibility.
It will be on Read-Only mode here.
Join the conversation on Discord
I am using the lifecycle to do some validating
And to create something after clicking a button
If you are making request, with file upload lifecycle is not going to help you much
lifecycle is a database event fired on entity creation
Ok, lifecycle doesn’t count here
I am having another issue
it won’t have the file in there
When I try to use multipart for something that has relations
Like an id, it should probably have connect in it, not sure how to structure the code that way
I am required to install some sort of puglin or configure something to have the files uploaded?
const formData = new FormData();
const data = {
…stuff,
relation: { id: relationId}
}
formData.append('body', JSON.stringify({ data })
I think body should have attribute data
with all the stuff inside
const data = {};
const formData = new FormData();
form.elements
.forEach(({ name, type, value, files, ...element }) => {
if (!['submit', 'file'].includes(type)) {
data[name] = value;
} else if (type === 'file') {
files.forEach((file) => {
formData.append(`files.${name}`, file, file.name);
});
}
});
formData.append('data', JSON.stringify(data));