DropZone JS check file size according to Document Type MVC 5 C#?

Viewed 30

I am new to dropzone js but this plugin looks amazing. I want to use dropzone js to save multiple files but according to the Document type. Means I have Doc Type like,

@Html.DropDownList("LKP_DocType", null, " ", htmlAttributes: new { @class = "form-control", @required = "required" })

Its Values for Document type are

                                ID  Name
                                1   Administrator
                                2   Manager
                                3   Programer

and I can only upload total of 5MB files per Document Type

Means I can upload only PDF type documents for administrator but total size of documents not exceed 5MB.

I already use Drop zone as mentioned in my code below where I am restricting PDF attachment 5MB.

Razor View

 <form action="~/Employees/Upload" class="dropzone " id="dropzoneJsForm" style="background-color: #7dc1d8">
            <div>  @Html.DropDownList("LKP_DocType", null, " ", htmlAttributes: new { @class = "form-control", @required = "required" })</div>
        </form>






   $(document).ready(function () {
        Dropzone.options.dropzoneJsForm =
            {
            autoProcessQueue: false,
                maxFilesize: 5,
                acceptedFiles: ".pdf",
                init: function () {
                    this.on("addedfile", function (file) {
                        var removeButton = Dropzone.createElement("<button class='btn btn-danger' style='margin-top:4%;border-radius: 12px;'>Remove</button>");
                        var _this = this;
                        removeButton.addEventListener("click", function (e) {
                            e.preventDefault();
                            e.stopPropagation();
                            _this.removeFile(file);
                        });
                        file.previewElement.appendChild(removeButton);
                    });
                }
            };
    });

Controller

     public ActionResult Upload()
            {
foreach (string fileName in Request.Files)
                {
                    HttpPostedFileBase file = Request.Files[fileName];


   }
 return Json(new
                {
                    Message = "Files Saved"
                });
    }

I am using autoProcessQueue: false to later add submit to send all files at once.

My question is how to add functionality of 5MB total document per Document type.

For example : I can add many PDF file for Administrator Document type But total file save not more than 5MB than I can add more document for another Document type. And also in Upload function i know which file are associated with which document type

Hopes for your suggestion Thanks

1 Answers

My suggestion is to GroupBy Request.Files by DocumentType and then check every group if files size is higher than 5MB.

You can try something like this:

var groups = Request.Files.GroupBy(f => f.ContentType);
foreach (var group in groups)
{
    if (group.Sum(f => f.ContentLength) > 5000000)
        // Do logic for more than 5MB
    else
        // Do logic for type under 5MB
}

NOTE: 5000000 is not exactly 5MB. You should do this check yourself

Related