I have object like this:
{
"name": "filename",
"size": 3523507,
"type": "image/png",
"extension": "png",
"url": "blob:http://localhost:8081/082e9c22-9869-4289-a9c8-e36b0640e61c"
}
And i need upload it to backend. I try it:
const session = axios.create({
baseURL: debug ? 'http://localhost:8000': '',
xsrfCookieName: CSRF_COOKIE_NAME,
xsrfHeaderName: CSRF_HEADER_NAME,
});
let formData = new FormData();
formData.append('file', file, 'file.name');
return session.post(`/chats/files/`,{...formData},), {
headers: {
"Content-Type": `multipart/form-data`,
}
}
But it doesn't work to add Blob to the formData
UPD I get an object with files from the send-message method in the vue-advanced-chat component in this form:
{
"name": "filename",
"size": 3523507,
"type": "image/png",
"extension": "png",
"localUrl": "blob:http://localhost:8081/aae047a9-5f9e-481f-b0cc-17febe954c31",
"blob": {}
}
Then I format it to display in the interface before uploading to the server
UPD2
I converted blob to file
send_file(roomId, messageId, blob_file, isAudio, duration) {
let formData = new FormData();
let file = new File([blob_file.blob], blob_file.name);
formData.append('file[]', file, blob_file.name);
return session.post(`/chats/files/`,
{'chat': roomId, 'message': messageId, ...formData},), {
headers: {
'Content-Type': `multipart/form-data; boundary=${formData._boundary}`,
},
timeout: 30000,
}
},
and still get: {"file":["No files were sent."]}
You are not sending data in correct way. It should be:
export const uploadFileRequest = ({ file }) => {
const data = new FormData();
data.append('file', file, file.name);
return session.post(`/chats/files/`, data, {
headers: {
'Content-Type': `multipart/form-data;boundary=${data._boundary}`,
},
timeout: 30000,
});
};
it should work, for reference https://medium.com/@fakiolinho/handle-blobs-requests-with-axios-the-right-way-bb905bdb1c04
After several days of searching, debugging and dozens of attempts, I managed to find a solution.
let file = new File([blob_file.blob], fileName);
let fileName = `${blob_file.name}.${blob_file.extension}`;
formData.append('file', file, fileName);
formData.append('chat', roomId);
formData.append('message', messageId);
multipart/form-data
, and don't specify _boundary - because in new versions of formData it is determined automatically!headers: {
'Content-Type': `multipart/form-data`,
},
so the final version of the code looks like this:
send_file(roomId, messageId, blob_file) {
let formData = new FormData();
let fileName = `${blob_file.name}.${blob_file.extension}`;
let file = new File([blob_file.blob], fileName);
formData.append('file', file, fileName);
formData.append('chat', roomId);
formData.append('message', messageId);
return session.post(`/chats/files/`,
formData, {
headers: {
'Content-Type': `multipart/form-data`,
},
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.