简体   繁体   English

验证上传文件大小

[英]Validate upload file size

I would like to check the file size, that is if the size exceed, it will prompt the UploadSizeError message.我想检查文件大小,即如果大小超过,它会提示 UploadSizeError 消息。 The method is CheckValidate().该方法是 CheckValidate()。

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

HTML 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();
          }
        });
      }
    };

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;
        }

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

JS 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";

    }



});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM