Answer the question
In order to leave comments, you need to log in
How to make an abortController of the desired request on an event in an array of requests?
I send several files with download progress and the ability to cancel each one by a button.
async sendFiles() {
const vue = this;
for (let i = 0; i < vue.inputFiles.length; i++) {
if (!vue.inputFiles[i].success && !vue.inputFiles[i].error) {
const controller = new AbortController();
const config = {
baseURL: '/api',
signal: controller.signal,
onUploadProgress: function (e) {
vue.inputFiles.splice(i, 1, { ...vue.inputFiles[i], progress: Math.round((e.loaded * 100) / e.total) });
},
};
this.abortControllers.push(controller);
setTimeout(() => controller.abort(), 1000)
const data = convertToFormData({ file: vue.inputFiles[i].file });
await this.sendFile(data, config, i);
}
}
}
async sendFile(data, config, index) {
const vue = this;
httpClient
.post(this.urlAdd, data, config)
.then((res) => {
vue.inputFiles[index].status = 'success';
vue.inputFiles[index].id = res.data.file.id;
vue.$emit('fileAdded', res.data)
})
.catch((err) => {
vue.inputFiles[index].status = 'error';
vue.inputFiles[index].error = err;
})
},
Answer the question
In order to leave comments, you need to log in
As a result, I did according to this article https://coderethinked.com/how-to-cancel-a-request-... , only created a cancel method inside the request creation cycle and added these methods to the { fileID: cancelMethod } format object so that from anywhere you can then use them.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question