【问题标题】:Validate upload file size验证上传文件大小
【发布时间】:2016-09-21 05:53:19
【问题描述】:

我想检查文件大小,如果超过,会提示UploadSizeError信息。方法是 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>

方法

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

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

回应

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

【问题讨论】:

  • 这个js函数是怎么触发的?
  • 点击 savetpApplicationFormDialog 时,会触发 savetpApplicationFormDialog 功能。它将检查验证,如果文件上传大小超过 1,它将触发 ProjectMessage.UploadSizeError。
  • this.files 返回一个 FileList,你必须选择它的一个条目(通常是索引 0 处的那个)

标签: javascript jquery model-view-controller


【解决方案1】:

试试这个

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

【讨论】:

  • 您好,感谢您的帮助。 idoffileinput 是 tpApplicationFormDialog_btnSave,我改了,还是不弹出消息。我的word文件是65.4kb。
  • 检查更新的答案。检查返回的尺寸是多少?
  • 我遇到了这个错误“无法读取未定义的属性'0'”
  • 我做了一些更改,它说“文件”未定义。
  • 首先,检查控制是否进入showConfirm,然后尝试使用以下代码 - var checkvalidation = CheckValidate() ;if(checkvalidation == true) { commonDialogs.showProgress(ProjectMessages.UploadSizeError); }
【解决方案2】:

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

    }



});

【讨论】:

  • 代码转储很少有帮助。此外,file.size 以字节为单位
  • 您好,感谢您的帮助。但我只想让函数检查文件是否大于大小,然后显示 projectmessage.uploadsizeerror 消息。
猜你喜欢
  • 2011-06-24
  • 2011-04-12
  • 2012-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-19
相关资源
最近更新 更多