0

I have a post method

uploadFile: async function () {
  const formData = new FormData();
  formData.append("file", this.file);
  let url = `http://...`;
  try {
    this.source = axios.CancelToken.source();
    const res = await axios.post(url, formData, {
      headers: {
        Authorization: "Token " + this.$store.getters.getToken,
        "Content-Type": "multipart/form-data",
      },
      cancelToken: this.source.token,
    });
  } catch (error) {
  }
}

and cancel method

  cancelLoad: function () {
      this.source.cancel('cancel upload');
  },

The request was canceled, but after reloading my page, the file uploaded. enter image description here

2
  • What exactly do you mean by "the file loaded"? Commented Oct 13, 2020 at 18:49
  • @tony19 the file was uploaded and I can get it from the backend Commented Oct 13, 2020 at 18:58

1 Answer 1

1

I've fixed it.

const CancelToken = axios.CancelToken;
let cancel;

const res = await axios.post(url, formData, {
      headers: {...},
      cancelToken: new CancelToken(function executor(c) {
        cancel = c;
      }),
}...

cancel();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.