Next/React File upload abortion in use cancellation

I am trying to create a Next JS video file uploader using Axios and NextApiRequests.
I want to implement a button to cancel an ongoing file upload once the handleUpload function is triggered but have no luck doing so.
The issue is that the file keeps uploading after the axios signal is aborted. As video files are pretty large it is far from optimal.

The setup is the following one :

upload.tsx

const inputRef = useRef(null);
const uploadController = new AbortController();
const [selectedFile, setSelectedFile] = useState(null);
const [progress, setProgress] = useState(0);
const [uploadStatus, setUploadStatus] = useState("select");

const clearFileInput = () => {
    if (uploadStatus == "uploading") {
        uploadController.abort();
    }

    inputRef.current.value = "";
    setSelectedFile(null);
    setProgress(0);
    setUploadStatus("select");
};

const onChooseFile = () => {
    inputRef.current.click();
}

const handleUpload = async (event: any) => {
        if (event.target.files && event.target.files.length > 0) {
            setSelectedFile(event.target.files[0]);

            try {
                setUploadStatus("uploading")
                const formData = new FormData();
                formData.append("myImage", event.target.files[0]);
                const { data } = await axios.post('/api/upload', formData,
                    {
                        onUploadProgress: ProgressEvent => {
                            const uploadProgress: number = parseFloat((ProgressEvent.loaded / (ProgressEvent.total || 1) * 100).toFixed(2))
                            setProgress((progress) => uploadProgress)
                        },
                        signal: uploadController.signal
                    }
                )
                setUploadStatus("done")
            } catch (error) {
                console.log(error)
            }
        }
    };

/api/upload.ts

import type { NextApiRequest, NextApiResponse } from 'next'
import formidable from 'formidable';
import path from 'path';
import fs from "fs/promises";

export const config = {
    api: {
        bodyParser: false,
    },
};


const handler = async (req: NextApiRequest, res: NextApiResponse) => {
    const options: formidable.Options = {}
    const uploadDir = path.join(process.cwd(), "/public/Assets/PendingUploads")
    options.uploadDir = uploadDir;
    options.filename = (name, ext, path, form) => {
        return Date.now().toString() + "_" + path.originalFilename;
    };
    options.maxFileSize = Infinity;

    const form = formidable(options)

    new Promise((resolve, reject) => {
        form.parse(req, (err, fields, file) => {
            if (err) reject(err)
            resolve({ fields, file })
        })

        form.on("file", (field, file) => {
            try {
                const uploadPath = path.join(uploadDir, path.basename(file.filepath).split(".").join(""))
                fs.mkdir(uploadPath)
                fs.rename(file.filepath, path.join(uploadPath, path.basename(file.filepath)))
            } catch (error) {
                console.log(error)
            }
        })
    })

    return res.status(200).json("OK")
}

export default handler;

I have tried :

  • Axios cancellation with signal/canceltoken but the api doesn’t get the cancellation
  • Event listeners and req.on statements with no luck
  • A few other things I did not keep track of…

Thank you for your help and time !

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật