Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6735457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:00:44+00:00 2026-05-26T11:00:44+00:00

I want to send file chunks to server async to server (MVC action in

  • 0

I want to send file chunks to server async to server (MVC action in a controller). I want to ensure that requests that I am sending through JS can be cancelled midway. I know about AJAX abort() but I think since the requests are async I am having little option other than setting a Session variable on MVC controller which rejects all blocks coming to it after being set. Please tell me the optimal way to do this. Can you tell me whether abort() can be used in this scenario and if yes then how? What are the other strategies I can follow to make this better?

My JS method is:

$.ajax({
    type: "POST",
    context: this,
    async: true,
    url: "/Home/PrepareMetaData",
    data: { "blocksCount": totalNumberOfBlocks, "fileName": file.name, "fileSize": file.size },
    dataType: "json",
    error: function () {
        statusLabel.innerHTML = 'Failed to send file meta data. Retry after some time.';
        progressElement.setAttribute('hidden', 'hidden');
    },
    success: function () {
        var start = 0;
        var end = Math.min(blockLength, file.size) - 1;
        var incrimentalIdentifier = 1;
        while (start <= file.size - 1 && !window.haltExecution) {
            var data = new FormData();
            data.append(incrimentalIdentifier, file.webkitSlice(start, end));
            xhr = $.ajax({
                async: true,
                url: '/Home/UploadBlock',
                context: this,
                data: data,
                cache: false,
                contentType: false,
                processData: false,
                type: 'POST',
                error: function (notice) {
                    statusLabel.innerHTML = notice;
                    progressElement.setAttribute('hidden', 'hidden');
                    haltExecution = true;
                },
                success: function (notice) {
                    blockCounter += 1;
                    if (notice.error || notice.isLastBlock) {
                        this.message = notice.message;
                    }

                    if (notice.error || blockCounter == totalNumberOfBlocks) {
                        statusLabel.innerHTML = this.message;
                        progressElement.setAttribute('hidden', 'hidden');
                        haltExecution = true;
                    }
                }
            });

            start = end + 1;
            end = Math.min(start + blockLength, file.size) - 1;
            incrimentalIdentifier++;
        }
    }
});

Thanks.

Edit:
I agree with the comments but if I make the Ajax request synchronous then I would be going against the nature of Ajax and would also be risk performance degrade. Another issue would be stuck UI. Even if I use Web Worker for this it would make my code async as it is now. What are your views??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:00:45+00:00Added an answer on May 26, 2026 at 11:00 am

    Here is a rewrite which should point you in the right direction. You need to send each subsequent chunk in the success handler of the previous one, rather than sending them all at once. This is just a quick hack around what you already had so it’s not perfect, but it should help.

    var sendFile = function(){
        var start = 0;
        var end = Math.min(blockLength, file.size) - 1;
        var incrimentalIdentifier = 1;
        // I assume the following variables are in scope already:
        // file, blockCounter, haltExecution
        var sendNextChunk = function(){
            var data = new FormData();
            data.append(incrimentalIdentifier, file.webkitSlice(start, end));
            xhr = $.ajax({
                async: true,
                url: '/Home/UploadBlock',
                context: this,
                data: data,
                cache: false,
                contentType: false,
                processData: false,
                type: 'POST',
                error: function (notice) {
                    statusLabel.innerHTML = notice;
                    progressElement.setAttribute('hidden', 'hidden');
                    haltExecution = true;
                },
                success: function (notice) {
                    blockCounter += 1;
                    if (notice.error || notice.isLastBlock) {
                        this.message = notice.message;
                    }
    
                    if (notice.error || blockCounter == totalNumberOfBlocks) {
                        statusLabel.innerHTML = this.message;
                        progressElement.setAttribute('hidden', 'hidden');
                        haltExecution = true;
                        return; // We are finished sending I assume?
                    }
    
                    start = end + 1;
                    end = Math.min(start + blockLength, file.size) - 1;
                    incrimentalIdentifier++;
    
                    // Send the next block.
                    sendNextChunk();
                }
            });
        };
    
        //Start sending:
        sendNextChunk();
    };
    
    $.ajax({
        type: "POST",
        context: this,
        async: true,
        url: "/Home/PrepareMetaData",
        data: { "blocksCount": totalNumberOfBlocks, "fileName": file.name, "fileSize": file.size },
        dataType: "json",
        error: function () {
            statusLabel.innerHTML = 'Failed to send file meta data. Retry after some time.';
            progressElement.setAttribute('hidden', 'hidden');
        },
        success: sendFile
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to send a file in chunks by calling a function that calls
I want to send a xml file to my wcf service , how can
I have a text file that I want to send over the network, this
I want to send a zip file through SOAP (from a SOAP client to
I want to write a webservice that will upload a file to our server
i want to send image file on my php server not upload on php
I want to send a file to the server in binary, not using the
i have a form that contains 3 File inputs.i want to send it via
I want to use the TcpClient and TcpListener to send an mp3 file over
I want so send every week an update by email. But Im afraid that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.