Skip to main content

Uploading large file in chunks in Mvc c# from Javascript ajax

Often we have a requirement to upload files in, Mvc c# application but when it comes to uploading larger file, we always think how to do it as uploading large file in one go have many challenges like UI responsiveness, If network fluctuate for a moment in between then uploading task get breaks and user have to upload it again etc.

In such cases uploading large file in chunks would be the best idea and have lots of advantages like
  • You can show uploading progress 
  • If uploading process get failed then you can resume uploading only rest of the file.
  • You can also implement pause/resume functionality by adding little extra code (not cover in this article).
Here i will show the whole process to uploading files in small chunks in Mvc c# application and to uploading in c# there will not be major changes you just need to implement the same code in .ashx (handler).

Given code covers 4 parts -
  • Drag and drop files
  • Slice file (create chunks) using JavaScript
  • Upload these chunks (called file blob) to server via ajax request (one chunk at a time)
  • Create and merge uploaded chunks into the file in Mvc C#

Drag and drop multiple files to web page

$(document).ready(function () {    
    'dragover dragenter'function (e) {
        $(e.currentTarget).css({ opacity: 0.5 });
    'drop'function (e) {
        //Upload file here
    'dragexit dragend dragleave'function (e) {
        $(e.currentTarget).css({ opacity: 1 });

  function onFileDrop(evt) {
    $(evt.currentTarget).css({ opacity: 1 });
    try {
        var files = evt.originalEvent.dataTransfer.files;
        for (var i = 0; i < files.length; i++) {
    } catch (e) {



Slice each file into small chunks, chunk size is given in "maxFileSizeKB" property you can change this, by default 100 KB is set in the given code and store these chunks into an array.

To make file name unique to avoid any data overriding, append random number in the file name and to get original file name in the server you can remove this random number from the name when needed.

Slice file in to small chunks (file blob) using JavaScript

function uploadFile(file) {
    //max file chunk size set to 100 KB change as per requirement.
    var maxFileSizeKB = 100;

    var fileChunks = [];
    var bufferChunkSizeInBytes = maxFileSizeKB * (1024);

    var currentStreamPosition = 0;
    var endPosition = bufferChunkSizeInBytes;
    var size = file.size;

    while (currentStreamPosition < size) {
        fileChunks.push(file.slice(currentStreamPosition, endPosition));
        currentStreamPosition = endPosition;
        endPosition = currentStreamPosition + bufferChunkSizeInBytes;

    //Append random number to file name to make it unique
    var fileName = Math.random() + "_" +;
    uploadFileChunk(fileChunks, fileName, 1, fileChunks.length);


Pass these array of chunks to the given function, current part index and unique file name, "uploadFileChunk" function calls it self recursively until the whole file uploaded,

Below are two method to upload files to the server, our example follows the first one -
  • Proceed to upload next chunk only when previous chunk get uploaded (get success response from the server). 
  • Send all chunk upload request at once and then wait for the response of each request (in this type of approach you need to append total part and current part number in the file name and save each file separately and when all file chunks get uploaded then merge these file into one and delete the separated chunk file.

Uploading file chunk to the server ( Mvc C#) by JavaScript ajax request

function uploadFileChunk(fileChunks, fileName, currentPart, totalPart) {
    var formData = new FormData();
    formData.append('file', fileChunks[currentPart - 1], fileName);

        type: "POST",
        url: '/FileManagement/UploadFileChunks',
        contentType: false,
        processData: false,
        data: formData,
        success: function (data) {
            if (totalPart >= currentPart) {
                console.log("uploading file part no: " + currentPart, " out of " + totalPart);
                if (data.status == true) {
                    if (totalPart == currentPart) {
                        //Whole file uploaded
                        console.log("whole file uploaded successfully");
                    } else {
                        //Show uploading progress
                        uploadFileChunk(fileChunks, fileName, currentPart + 1, totalPart);
                } else {
                    //retry message to upload rest of the file
                    console.log("failed to upload file part no: " + currentPart);
        error: function () {
            //retry message to upload rest of the file
            console("error to upload file part no: " + currentPart);


Server side code to merge uploaded file chunks, in the below code if file is not created then file get created and chunks get merged next time into this file.

Server side code to merge the uploaded file chunks.

public class FileManagement : Controller

  public JsonResult UploadFileChunks()
    var files = Request.Files;

    if (files.Count > 0)
            string filePath = Path.Combine(GetUploadPath(), files[0].FileName);

            using (FileStream fs = new FileStream(filePath, FileMode.Append))
                var bytes = GetBytes(files[0].InputStream);
                fs.Write(bytes, 0, bytes.Length);

            return Json(new { status = true });
        catch (Exception ex)
            return Json(new { status = false, message = ex.Message });

    return Json(new { status = false });

  private byte[] GetBytes(Stream input)
    byte[] buffer = new byte[input.Length];
    using (MemoryStream ms = new MemoryStream())
        int read;
        while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
            ms.Write(buffer, 0, read);

        return ms.ToArray();

  private string GetUploadPath()
    var rootPath = System.Web.Hosting.HostingEnvironment.MapPath("~/UploadedFiles/" + Session.SessionID);

    if (!Directory.Exists(rootPath))

    return rootPath;

Popular posts from this blog

how $document.ready() is different from window.onload()

we often use window.onload() in javascript and $document.ready() in jquery and assume that both are same just jquery library wrap up javascript window.onload() and introduce $document.ready() but no there is much difference between them below is the explanation -

Merging multiple PDFs using iTextSharp in c#

In this article i will show you how to merge multiple pdfs into one using ITextSharp below is the two approach one is to pass your input files path, output file path (will be created if not exist) and another is pass direct input stream, output stream and it will write the merge files into output stream.

How to handle click event of linkbutton inside gridview

Recently I have posted how to sort only current page of gridview , Scrollble gridview with fixed header through javascript , File upload control inside gridview during postback and now i am going to explain how to handle click event of linkbutton or any button type control inside gridview. We can handle click event of any button type control inside gridview by two way first is through event bubbling and second one is directly (in this type of event handling we need to access current girdviewrow container)

How to validate dropdownlist in JavaScript

In this article you will see how to put validation in dropdownlist by javascript, suppose first item value of dropdownlist is 0 and text is "-Select-" just like given below and we have to validate that at least one item is selected excluding default i.e "-Select-".