user3807187
user3807187

Reputation: 185

Validate upload file size

I would like to check the file size, that is if the size exceed, it will prompt the UploadSizeError message. The method is CheckValidate().

 var dialogButtons = [
      {
        text: "Upload",
        id: "tpApplicationFormDialog_btnSave",
        disabled: "disabled",
        class: "requiredAtt1",
        click: savetpApplicationFormDialog
      },

HTML

<button type="button" id="tpApplicationFormDialog_btnSave" class="requiredAtt1 ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" aria-disabled="false"><span class="ui-button-text">Upload</span></button>
<span class="ui-button-text">Upload</span>

Method

 function CheckValidate() {
        var o = getOptions();
        var f = this.files

        var ret = false;
        if (f.size> 1 || f.fileSize > 1)
        {
            ret = true;
        }
        return ret;
        }

Response

function savetpApplicationFormDialog() {
      if (hasChanges()) {
          commonDialogs.showConfirm(ProjectMessages.ConfirmUpload, function () {
              if(CheckValidate()) {
                  commonDialogs.showProgress(ProjectMessages.UploadSizeError);
              }
                  try {
            commonDialogs.showProgress(ProjectMessages.ProgressUpload);
            var o = getOptions();
            var form = $(o.form);
            form.ajaxSubmit(handleResponse);
          } catch (e) {
            commonDialogs.showError();
          }
        });
      }
    };

Upvotes: 1

Views: 138

Answers (2)

Chetan Gawai
Chetan Gawai

Reputation: 2401

try this

function CheckValidate() {
        var o = getOptions();
        //var f = this.files
         var f=  $("#idoffileinput").get(0).files[0]['size'] //will give file size in bytes
        var ret = false;
        var maxSize = xxx ;//max size in bytes
        if (f> maxSize )
        {
            ret = true;
        }
        return ret;
        }

Upvotes: 1

Felix Fong
Felix Fong

Reputation: 985

JSBin: http://jsbin.com/lezilu/edit?js,output

JS

let input = document.querySelector(".file");
let show = document.querySelector(".show");

input.addEventListener("change", e => {

    let file = e.target.files[0];
    let fileName = file.name;
    let fileSize = file.size;

    if(fileSize < 1000){

        // Less then 1MB

        show.textContent = "Pass";


    }else{

        // Bigger then 1 MB
        show.textContent = "Chose again";

    }



});

Upvotes: 0

Related Questions